[Python-ideas] os.path.join

MRAB python at mrabarnett.plus.com
Wed Oct 30 19:25:04 CET 2013


On 30/10/2013 17:06, Bruce Leban wrote:
> 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'
>
Shouldn't that be '\\\\abc\\x\\y'?



More information about the Python-ideas mailing list