Skip to content

Value error 'path is on drive c: start on drive d:' in os.path.relpath #51444

Closed

Description

@jaraco
BPO 7195
Nosy @jaraco, @ncoghlan

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 = None
closed_at = <Date 2009-10-31.05:00:30.541>
created_at = <Date 2009-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 = <Date 2009-11-01.01:23:20.069>
user = 'https://github.com/jaraco'

bugs.python.org fields:

activity = <Date 2009-11-01.01:23:20.069>
actor = 'ncoghlan'
assignee = 'none'
closed = True
closed_date = <Date 2009-10-31.05:00:30.541>
closer = 'ncoghlan'
components = ['Windows']
creation = <Date 2009-10-23.21:39:22.140>
creator = 'jaraco'
dependencies = []
files = []
hgrepos = []
issue_num = 7195
keywords = []
message_count = 6.0
messages = ['94394', '94744', '94755', '94758', '94759', '94780']
nosy_count = 2.0
nosy_names = ['jaraco', 'ncoghlan']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue7195'
versions = ['Python 2.6', 'Python 3.1']

Activity

  1. jaraco commented on Oct 23, 2009

    @jaraco
    MemberAuthor

    A simple test fails:

    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.

  2. added
    type-bug
    on Oct 23, 2009
  3. ncoghlan commented on Oct 31, 2009

    @ncoghlan
    Contributor

    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.

  4. jaraco commented on Oct 31, 2009

    @jaraco
    MemberAuthor

    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.

  5. ncoghlan commented on Oct 31, 2009

    @ncoghlan
    Contributor

    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.

  6. jaraco commented on Oct 31, 2009

    @jaraco
    MemberAuthor

    The documentation changes I suggested make no mention to Windows pathing
    quirks. They instead clarify two aspects:

    1. cross-platform behavior (how the path is interpreted) and
    2. 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.

  7. ncoghlan commented on Nov 1, 2009

    @ncoghlan
    Contributor

    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).

  8. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowstype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions