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
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.
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”.
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'.
Activity
ericvsmith commentedon Dec 11, 2022
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”.
hoba87 commentedon Dec 11, 2022
On Windows that is no absolute string. It has no meaning on Windows.
eryksun commentedon Dec 11, 2022
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, sor"\foo"is a relative path. I don't know how to fixntpath.isabs()without breaking existing code, other than to introduce a new function such asos.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").join(r'C:\spam', r'D:\eggs') == r'D:\eggs'.join(r'C:\spam', r'\eggs') == r'C:\eggs'.join(r'C:\spam', 'C:eggs') == r'C:\spam\eggs'.maurycy commentedon Dec 5, 2026
Fixed since 53455a3 (3.12, #100811):
cpython/Doc/library/os.path.rst
Lines 396 to 404 in f9704be
cc @StanFromIreland