[issue19456] ntpath doesn't join paths correctly when a drive is present

Bruce Leban report at bugs.python.org
Mon Nov 4 01:27:10 CET 2013


Bruce Leban added the comment:

A non-UNC windows path consists of two parts: a drive and a conventional path. If the drive is left out, it's relative to the current drive. If the path part does not have a leading \ then it's relative to the current path on that drive. Note that Windows has a different working dir for every drive.

x\y.txt    # in dir x in current dir on current drive
\x\y.txt   # in dir x at root of current drive
E:x\y.txt  # in dir in current dir on drive E
E:\x\y.txt # in dir x at root of drive E

UNC paths are similar except \\server\share is used instead of X: and there are no relative paths, since the part after share always starts with a \.

Thus when joining paths, if the second path specifies a drive, then the result should include that drive, otherwise the drive from the first path should be used. The path parts should be combined with the standard logic.

Some additional test cases

tester("ntpath.join(r'C:/a/b/c/d', '/e/f')", 'C:\e\f')
tester("ntpath.join('//a/b/c/d', '/e/f')", '//a/b/e/f')
tester("ntpath.join('C:x/y', r'z')", r'C:x/y/z')
tester("ntpath.join('C:x/y', r'/z')", r'C:/z')

Andrei notes that the following is wrong but wonders what the correct answer is:

>>> ntpath.join('C:/a/b', 'D:x/y')
'C:/a/b\\D:x/y'

The /a/b part of the path is an absolute path on drive C and isn't "transferable" to another drive. So a reasonable result is simply 'D:x/y'. This matches Windows behavior. If on Windows you did

$ cd /D C:\a\b
$ cat D:x\y

it would ignore the current drive on C set by the first command and use the current drive on D.

tester("ntpath.join('C:/a/b', 'D:x/y')", r'D:x/y')
tester("ntpath.join('//c/a/b', 'D:x/y')", r'D:x/y')

----------
nosy: +Bruce.Leban

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19456>
_______________________________________


More information about the Python-bugs-list mailing list