Relative versus absolute paths on Windows

Christian Heimes lists at cheimes.de
Mon Nov 23 20:23:19 EST 2009


Gregory Ewing wrote:
>>>>> ntpath.join('d:\\foo', '\\bar')
>> '\\bar'
> 
> This does seem like a bug, though -- the correct result
> should really be 'd:\\bar', since that's what you would
> get if you used the name '\\bar' with 'd:' as your current
> drive.

No, it's not a bug. Since \bar is an absolute path, all path segments
before the absolute path are ignored. This is documented at
http://docs.python.org/library/os.path.html#os.path.join

>>> ntpath.isabs("\\bar")
True
>>> ntpath.join("ignored", "\\bar")
'\\bar'

Posixpath follows the same rules, too.

>>> posixpath.join("..", "egg", "/var")
'/var'
>>> posixpath.join("..", "egg", "var")
'../egg/var'

Christian




More information about the Python-list mailing list