Skip to content

Clarify os.path.join documentation #100783

Closed
@hauntsaninja

Description

@hauntsaninja

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

Metadata

Metadata

Assignees

Labels

3.10only security fixes3.11only security fixes3.12only security fixesOS-windowsdocsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions