Relative versus absolute paths on Windows

Ethan Furman ethan at stoneleaf.us
Mon Nov 23 20:44:46 EST 2009


Christian Heimes wrote:
> 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

Given that it's documented to work that way, I'll agree that this is not 
a bug -- however, that doesn't mean it's /right/.

 From the documentation:
Join one or more path components *intelligently*.

To me, that means taking into account the bizarre in the MS world of 
multiple roots on a system... I know I have looked at os.path.join a 
couple times also, and found the documented behavior to be completely 
unsatisfactory.  In the MS world a drive letter specifier should trump a 
mere backslash, not be trumped by it.

Are there real world situations where the reverse is desired?

>>>>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'

Posix has the luxury of running on sane systems with only one root.

~Ethan~



More information about the Python-list mailing list