[Python-ideas] os.path.join

Bruce Leban bruce at leapyear.org
Wed Oct 30 18:06:03 CET 2013


I don't know if the code is wrong but if you're asking if the *result* of
join is wrong, I don't think it is. It references the same file as these
commands:

cd /static
cat /styles/largestyles,css

I agree it might be confusing but it's pretty explicitly documented. On the
other hand, this is also documented and it's wrong by the above standard

>>> os.path.join(r'c:\abc', r'\def\g')   # Windows paths
'\\def\\g'

On Windows \def\g is a drive-relative path not an absolute path. To get the
right result you need to do:

>>> drive, path = os.path.splitdrive(r'c:\abc')
>>> drive + os.path.join(path, r'/def/g')
'c:/def/g'

This works even on systems that don't use drive letters. It would be nice
if there was a less clumsy way to do this. It's worse than that because it
also screws up UNC paths

>>> os.path.join(r'\\abc\def\ghi', r'\x\y')
'\\x\\y'

The result references a UNC share of \\x\y rather than a directory of x
which is also wrong. It would be nice if there was a simpler way to get
this right:

>>> os.path.join(r'c:\abc', r'\x\y', keep_drive_unc=True)
'c:\\x\\y'
>>> os.path.join(r'\\abc\def\ghi', r'\x\y', keep_drive_unc=True)
'\\\\abc\\def\\x\\y'


--- Bruce
I'm hiring: http://www.cadencemd.com/info/jobs
Latest blog post: Alice's Puzzle Page http://www.vroospeak.com
Learn how hackers think: http://j.mp/gruyere-security


On Wed, Oct 30, 2013 at 9:34 AM, anatoly techtonik <techtonik at gmail.com>wrote:

>   >>> os.path.join('/static', '/styles/largestyles.css')
>   '/styles/largestyles.css'
>
> Is it only me who thinks that the code above is wrong?
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131030/8ab87354/attachment.html>


More information about the Python-ideas mailing list