You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Value error 'path is on drive c: start on drive d:' in os.path.relpath #51444
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2009-10-31.05:00:30.541>created_at=<Date2009-10-23.21:39:22.140>labels= ['type-bug', 'invalid', 'OS-windows']
title="Value error 'path is on drive c: start on drive d:' in os.path.relpath"updated_at=<Date2009-11-01.01:23:20.069>user='https://github.com/jaraco'
Python 2.6.3 (r263rc1:75186, Oct 2, 2009, 20:40:30) [MSC v.1500 32 bit
(Intel)] on win32
>>> import os
>>> os.path.relpath('\\bar', 'd:\\')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\python\lib\ntpath.py", line 487, in relpath
% (path_list[0], start_list[0]))
ValueError: path is on drive C:, start on drive d:
If I change the current directory to 'E:\', the error changes to "path
is on drive E:, start on drive d:".
Clearly, relpath is doing some calculations based on the current
directory, although the documentation states that it should be
performing a relative path calculation based on the supplied start
("D:\" in this case).
In Python 3.1.1, the error is "path is on mount 'C:', start on mount 'd:'"
os.path.relpath should be able to perform relative path calculations
regardless of the current directory, or the documentation should explain
why the current directory is relevant when start is supplied.
It's not the current directory that's the problem, it's the current
drive. Windows has no absolute root directory - instead, it has a root
directory for each drive. That means that "\\dir" is a path relative to
the current drive rather than an absolute path. You need to put the
drive letter in there to make it an absolute path.
That's just a fact of life when working with the windows file system,
rather than anything specific to Python or os.path.relpath.
Based on the response, then the documentation is inadequate. I don't
want to make a stink about this, but I think the issue is still
unresolved. If it would be better to discuss this elsewhere, please advise.
The documentation says "Return a relative filepath to path either from
the current directory or from an optional start point".
The documentation should say "Return a relative filepath to a path,
where path is considered relative to the current directory, either from
the current directory or from an optional start point. On Windows, a
ValueError is raised if the current directory and the start path are not
on the same drive."
The clarification is that the path specified is _not_ relative to the
start point (which would have been my guess), but is relative to another
unspecified environmental condition (the current directory). To leave
out this clarification means that this implicit behavior is left to the
user to discover rather than to state that it's by design.
For my purposes, I wanted a function that would calculate a target based
on a start path and a relative or absolute path from it. Based on the
documentation, I thought relpath was it.
I understand better now what the purpose of relpath is, and it's not
what I was expecting. I think the documentation could be improved to
help manage this expectation for other users.
The path *returned* is relative to the start point. The target path is
figured out normally (i.e. relative to the current directory if an
absolute path is not given).
In your example, you aren't passing in an absolute Windows path - you
give a path relative to the current drive (since no drive is specified).
Windows file pathing is hopeless, but it isn't the job of Python's
documentation to explain its quirks.
The documentation changes I suggested make no mention to Windows pathing
quirks. They instead clarify two aspects:
cross-platform behavior (how the path is interpreted) and
platform-specific implementation of relpath (what Python exceptions
to expect in exceptional conditions).
These two changes would have made it clear to me from the beginning that
relpath is not what I was searching for when I wanted to find a path
from one path to another. I'm just trying to help those who come after
me to not run into the same situation.
os.relpath *does* give you a relative path between two directories.
The problem you are encountering is that, on Windows, a relative path
doesn't even *exist* if the two directories are on different drives
(which is exactly what the error message says).
Activity
jaraco commentedon Oct 23, 2009
A simple test fails:
If I change the current directory to 'E:\', the error changes to "path
is on drive E:, start on drive d:".
Clearly, relpath is doing some calculations based on the current
directory, although the documentation states that it should be
performing a relative path calculation based on the supplied start
("D:\" in this case).
In Python 3.1.1, the error is "path is on mount 'C:', start on mount 'd:'"
os.path.relpath should be able to perform relative path calculations
regardless of the current directory, or the documentation should explain
why the current directory is relevant when start is supplied.
ncoghlan commentedon Oct 31, 2009
It's not the current directory that's the problem, it's the current
drive. Windows has no absolute root directory - instead, it has a root
directory for each drive. That means that "\\dir" is a path relative to
the current drive rather than an absolute path. You need to put the
drive letter in there to make it an absolute path.
That's just a fact of life when working with the windows file system,
rather than anything specific to Python or os.path.relpath.
jaraco commentedon Oct 31, 2009
Based on the response, then the documentation is inadequate. I don't
want to make a stink about this, but I think the issue is still
unresolved. If it would be better to discuss this elsewhere, please advise.
The documentation says "Return a relative filepath to path either from
the current directory or from an optional start point".
The documentation should say "Return a relative filepath to a path,
where path is considered relative to the current directory, either from
the current directory or from an optional start point. On Windows, a
ValueError is raised if the current directory and the start path are not
on the same drive."
The clarification is that the path specified is _not_ relative to the
start point (which would have been my guess), but is relative to another
unspecified environmental condition (the current directory). To leave
out this clarification means that this implicit behavior is left to the
user to discover rather than to state that it's by design.
For my purposes, I wanted a function that would calculate a target based
on a start path and a relative or absolute path from it. Based on the
documentation, I thought relpath was it.
I understand better now what the purpose of relpath is, and it's not
what I was expecting. I think the documentation could be improved to
help manage this expectation for other users.
ncoghlan commentedon Oct 31, 2009
The path *returned* is relative to the start point. The target path is
figured out normally (i.e. relative to the current directory if an
absolute path is not given).
In your example, you aren't passing in an absolute Windows path - you
give a path relative to the current drive (since no drive is specified).
Windows file pathing is hopeless, but it isn't the job of Python's
documentation to explain its quirks.
jaraco commentedon Oct 31, 2009
The documentation changes I suggested make no mention to Windows pathing
quirks. They instead clarify two aspects:
to expect in exceptional conditions).
These two changes would have made it clear to me from the beginning that
relpath is not what I was searching for when I wanted to find a path
from one path to another. I'm just trying to help those who come after
me to not run into the same situation.
ncoghlan commentedon Nov 1, 2009
os.relpath *does* give you a relative path between two directories.
The problem you are encountering is that, on Windows, a relative path
doesn't even *exist* if the two directories are on different drives
(which is exactly what the error message says).