Skip to content

os.path.join does not work correctly #100172

Done

Description

@hoba87

Bug report

This simple script gives as a result g:\\2020 and not as expected g:\\fotos\\2020. It seems that the '\\' in front of 2020 is interpreted as '..\\', because without it the output is as expected.

import os
os.path.join('g:\\fotos', '\\2020')

Your environment

Windows 10 64 bit
python 3.10.9 (and 3.7)

Activity

  1. added
    type-bug
    on Dec 11, 2022
  2. ericvsmith commented on Dec 11, 2022

    @ericvsmith
    Member

    From the documentation: “If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.”

    Your second path is absolute.

    Also from the documentation: “On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered.”

    So this is working as documented.

    And note that your strings only have a single slash in the. You’d be better off with raw strings, like r”\2020”.

  3. hoba87 commented on Dec 11, 2022

    @hoba87
    Author

    On Windows that is no absolute string. It has no meaning on Windows.

  4. eryksun commented on Dec 11, 2022

    @eryksun
    Contributor

    The documentation of os.path.join() should be changed to read "On Windows, the drive is not reset when a path component with a root (e.g., r'\foo') is encountered". The current text mistakenly uses "drive letter" and "absolute".

    Python's implementation of ntpath.isabs() and some documentation is mistaken. A path without a drive on Windows is relative to the drive of the current working directory, so r"\foo" is a relative path. I don't know how to fix ntpath.isabs() without breaking existing code, other than to introduce a new function such as os.path.isabsolute() that behaves correctly.


    The behavior you're reporting in ntpath.join() is intentional. A current drive is tracked as the items are joined, for which the drive concept includes logical drive-letter drives (e.g. "C:"), UNC drives (e.g. "\\server\share"), UNC device drives (e.g. "\\?\UNC\server\share") and all other device drives (e.g. "\\.\PIPE" or "\\?\BootPartition").

    • An item with a different drive replaces the current path. For example, join(r'C:\spam', r'D:\eggs') == r'D:\eggs'.
    • An item with a root and with no drive replaces everything except the drive. For example, join(r'C:\spam', r'\eggs') == r'C:\eggs'.
    • An item without a root and with a drive that matches the current drive joins the relative path on the drive. For example, join(r'C:\spam', 'C:eggs') == r'C:\spam\eggs'.
  5. added
    docs
    on Dec 14, 2022
  6. maurycy commented on Dec 5, 2026

    @maurycy
    Contributor

    Fixed since 53455a3 (3.12, #100811):

    On Windows, the drive is not reset when a rooted path segment (e.g.,
    ``r'\foo'``) is encountered. If a segment is on a different drive or is an
    absolute path, all previous segments are ignored and the drive is reset. For
    example::
    >>> os.path.join('c:\\', 'foo')
    'c:\\foo'
    >>> os.path.join('c:\\foo', 'd:\\bar')
    'd:\\bar'

    cc @StanFromIreland

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

    docstype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions