Relative versus absolute paths on Windows

Ethan Furman ethan at stoneleaf.us
Fri Nov 20 15:52:50 EST 2009


Jason R. Coombs wrote:
> The current implementation of Python (2.6.4, 3.1.1) treats \bar as a
> relative path but reports it as an absolute path.
> 
> 
>>>>ntpath.isabs('\\bar')
> 
> True
> 
>>>>ntpath.abspath('\\bar')
> 
> 'C:\\bar'
> 
>>>>os.chdir('d:\\')
>>>>ntpath.abspath('\\bar')
> 
> 'd:\\bar'
> 
>>>>os.chdir('\\\\server\\share')
>>>>ntpath.abspath('\\bar')
> 
> '\\\\server\\share\\bar'
> 
> In other words, paths without a drive letter are reported as absolute,
> but treated as relative, except in a few special cases.
> 
> 
>>>>ntpath.join('d:\\foo', '\\bar')
> 
> '\\bar'
> 
> In this case, \bar is treated as absolute, and not relative to d:\foo.

It is often said on this list that 'Python is not Java'.  It is also 
true that 'Windows is not Unix'.

Unlike the *nix world where there is a *single* root, and everything 
else is relative to that, in the Windows world there are several roots 
-- every drive has one!

So \bar is both an absolute path on whichever drive is active (or 
specified), as well as relative if you happen to have more than one 
drive, but it will not ever be relative on any specific drive.  If you 
want 'bar' to be relative to the current directory, do not specify a 
leading '\'.

[snip]

> Ultimately, I don't care what the definition is. It seems to me,
> however, that Python should have a function that can resolve one path
> name relative to another, but due to these limitations, it does not. I
> should point out that os.path.relpath is not the solution either as
> the first parameter is always treated as relative to the current
> directory (more info at http://bugs.python.org/issue7195).

Please note that there *is not* a relative path that can take you from 
c:\foo to d:\bar  (see the point about multiple roots above).

> I've built workarounds in https://svn.jaraco.com/jaraco/python/jaraco.windows/jaraco/windows/filesystem.py
> as join() and resolve_path(). I'm happy to continue using these
> workarounds, but I wanted to bring this issue to the attention of the
> community for any comments or suggestions or answers to the following
> questions.
> 
> What is the benefit of treating \path as absolute?

That was not Python's decision, but Windows'.

> Should Python have built-in support for resolving one path relative to
> another (such as is jaraco.windows.filesystem.resolve_path does)?

As I noted above, you are not returning a relative path when the paths 
cross drive letter boundaries, or one of the paths specified is a 
drive-absolute path (such as '\bar').

> Given the long established behavior of Python and other platforms for
> handling absolute paths in Windows, is there a way forward that
> handles these cases more elegantly, or is the best approach to just
> mumble something nasty under our breath and work around these issues
> on a case-by-case basis?

I just go with the mumbling, myself.

Hope this helps.

~Ethan~

P.S.
And now that I look up the comments in the bug-tracker, I see this was 
all already pointed out to you.



More information about the Python-list mailing list