Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify os.path.join documentation #100783

Closed
hauntsaninja opened this issue Jan 6, 2023 · 0 comments
Closed

Clarify os.path.join documentation #100783

hauntsaninja opened this issue Jan 6, 2023 · 0 comments
Assignees
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes docs Documentation in the Doc dir OS-windows type-bug An unexpected behavior, bug, or error

Comments

@hauntsaninja
Copy link
Contributor

hauntsaninja commented Jan 6, 2023

https://docs.python.org/3/library/os.path.html#os.path.join

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

There are a couple potential issues here.

First, it should be "drive", not "drive letter":

>>> ntpath.join("thrownaway", "//host/computer/dir", "/asdf")  # unc is not a letter
'//host/computer/asdf'

Second, it should be "if a component is from a different drive or an absolute path, all previous components are thrown away and the drive is reset":

>>> ntpath.join("C:", "foo", "C:", "bar")  # previous components are not thrown away
'C:foo\\bar'
>>> ntpath.join("C:", "foo", "D:", "bar")
'D:bar'

Third, as a nit, maybe "component" should be replaced with "segment", since arguments can contain path separators. This would improve consistency with the pathlib docs.

cc @barneygale @JelleZijlstra

These came up in #100782

Linked PRs

@hauntsaninja hauntsaninja added the docs Documentation in the Doc dir label Jan 6, 2023
@hauntsaninja hauntsaninja self-assigned this Jan 6, 2023
hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Jan 6, 2023
- Use "drive", not "drive letter", because of UNC paths
- Previous components are not thrown away from relative drive letters
- Use "segment" instead of "component" for consistency with pathlib

See issue for more details
@eryksun eryksun added type-bug An unexpected behavior, bug, or error OS-windows 3.11 only security fixes 3.10 only security fixes 3.12 bugs and security fixes labels Jan 7, 2023
hauntsaninja added a commit that referenced this issue Jan 8, 2023
- Use "drive", not "drive letter", because of UNC paths
- Previous components are not thrown away from relative drive letters
- Use "segment" instead of "component" for consistency with pathlib
- Other miscellaneous improvements
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 8, 2023
- Use "drive", not "drive letter", because of UNC paths
- Previous components are not thrown away from relative drive letters
- Use "segment" instead of "component" for consistency with pathlib
- Other miscellaneous improvements
(cherry picked from commit 53455a3)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 8, 2023
- Use "drive", not "drive letter", because of UNC paths
- Previous components are not thrown away from relative drive letters
- Use "segment" instead of "component" for consistency with pathlib
- Other miscellaneous improvements
(cherry picked from commit 53455a3)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
miss-islington added a commit that referenced this issue Jan 8, 2023
- Use "drive", not "drive letter", because of UNC paths
- Previous components are not thrown away from relative drive letters
- Use "segment" instead of "component" for consistency with pathlib
- Other miscellaneous improvements
(cherry picked from commit 53455a3)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
miss-islington added a commit that referenced this issue Jan 8, 2023
- Use "drive", not "drive letter", because of UNC paths
- Previous components are not thrown away from relative drive letters
- Use "segment" instead of "component" for consistency with pathlib
- Other miscellaneous improvements
(cherry picked from commit 53455a3)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes docs Documentation in the Doc dir OS-windows type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants