Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,38 @@ Pure path objects provide path-handling operations which don't actually
access a filesystem. There are three ways to access these classes, which
we also call *flavours*:

.. class:: PurePath(*pathsegments)
.. class:: PurePath(*pathcomponents)

A generic class that represents the system's path flavour (instantiating
it creates either a :class:`PurePosixPath` or a :class:`PureWindowsPath`)::

>>> PurePath('setup.py') # Running on a Unix machine
PurePosixPath('setup.py')

Each element of *pathsegments* can be either a string representing a
path segment, an object implementing the :class:`os.PathLike` interface
Each element of *pathcomponents* can be either a string representing a
path component, an object implementing the :class:`os.PathLike` interface
which returns a string, or another path object::

>>> PurePath('foo', 'some/path', 'bar')
PurePosixPath('foo/some/path/bar')
>>> PurePath(Path('foo'), Path('bar'))
PurePosixPath('foo/bar')

When *pathsegments* is empty, the current directory is assumed::
When *pathcomponents* is empty, the current directory is assumed::

>>> PurePath()
PurePosixPath('.')

When several absolute paths are given, the last is taken as an anchor
(mimicking :func:`os.path.join`'s behaviour)::
If a component is an absolute path, all previous components are thrown away
Comment thread
hauntsaninja marked this conversation as resolved.
Outdated
(like :func:`os.path.join`')::
Comment thread
hauntsaninja marked this conversation as resolved.
Outdated

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')
>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')

However, in a Windows path, changing the local root doesn't discard the
previous drive setting::
On Windows, the drive letter is not reset when a drive-less absolute path

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the drive necessarily one letter?

@hauntsaninja hauntsaninja Jan 6, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I think the drive can be a UNC path. This also would need to get fixed in the os.path.join documentation: https://docs.python.org/3/library/os.path.html#os.path.join

>>> PureWindowsPath("hello", "//host/computer/dir", "/asdf")
PureWindowsPath('//host/computer/asdf')

>>> ntpath.join("hello", "//host/computer/dir", "/asdf")
'//host/computer/asdf'

I'll update to "drive" and open a second PR for os.path.join if @barneygale concurs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"drive" makes more sense to me too.

component (e.g., `r'\foo'`) is encountered::

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
Expand Down Expand Up @@ -155,17 +155,17 @@ we also call *flavours*:
.. versionchanged:: 3.6
Added support for the :class:`os.PathLike` interface.

.. class:: PurePosixPath(*pathsegments)
.. class:: PurePosixPath(*pathcomponents)

A subclass of :class:`PurePath`, this path flavour represents non-Windows
filesystem paths::

>>> PurePosixPath('/etc')
PurePosixPath('/etc')

*pathsegments* is specified similarly to :class:`PurePath`.
*pathcomponents* is specified similarly to :class:`PurePath`.

.. class:: PureWindowsPath(*pathsegments)
.. class:: PureWindowsPath(*pathcomponents)

A subclass of :class:`PurePath`, this path flavour represents Windows
filesystem paths, including `UNC paths`_::
Expand All @@ -175,7 +175,7 @@ we also call *flavours*:
>>> PureWindowsPath('//server/share/file')
PureWindowsPath('//server/share/file')

*pathsegments* is specified similarly to :class:`PurePath`.
*pathcomponents* is specified similarly to :class:`PurePath`.

.. _unc paths: https://en.wikipedia.org/wiki/Path_(computing)#UNC

Expand Down Expand Up @@ -212,10 +212,10 @@ Paths of a different flavour compare unequal and cannot be ordered::
Operators
^^^^^^^^^

The slash operator helps create child paths, mimicking the behaviour of
:func:`os.path.join`. For instance, when several absolute paths are given, the
last is taken as an anchor; for a Windows path, changing the local root doesn't
discard the previous drive setting::
The slash operator helps create child paths, like :func:`os.path.join`.
If the argument is an absolute path, the previous path components are thrown away.
Comment thread
hauntsaninja marked this conversation as resolved.
Outdated
On Windows, the drive letter is not reset when a drive-less absolute path
component (e.g., `r'\foo'`) is encountered::

>>> p = PurePath('/etc')
>>> p
Expand Down Expand Up @@ -689,7 +689,7 @@ Concrete paths are subclasses of the pure path classes. In addition to
operations provided by the latter, they also provide methods to do system
calls on path objects. There are three ways to instantiate concrete paths:

.. class:: Path(*pathsegments)
.. class:: Path(*pathcomponents)

A subclass of :class:`PurePath`, this class represents concrete paths of
the system's path flavour (instantiating it creates either a
Expand All @@ -698,27 +698,27 @@ calls on path objects. There are three ways to instantiate concrete paths:
>>> Path('setup.py')
PosixPath('setup.py')

*pathsegments* is specified similarly to :class:`PurePath`.
*pathcomponents* is specified similarly to :class:`PurePath`.

.. class:: PosixPath(*pathsegments)
.. class:: PosixPath(*pathcomponents)

A subclass of :class:`Path` and :class:`PurePosixPath`, this class
represents concrete non-Windows filesystem paths::

>>> PosixPath('/etc')
PosixPath('/etc')

*pathsegments* is specified similarly to :class:`PurePath`.
*pathcomponents* is specified similarly to :class:`PurePath`.

.. class:: WindowsPath(*pathsegments)
.. class:: WindowsPath(*pathcomponents)

A subclass of :class:`Path` and :class:`PureWindowsPath`, this class
represents concrete Windows filesystem paths::

>>> WindowsPath('c:/Program Files/')
WindowsPath('c:/Program Files')

*pathsegments* is specified similarly to :class:`PurePath`.
*pathcomponents* is specified similarly to :class:`PurePath`.

You can only instantiate the class flavour that corresponds to your system
(allowing system calls on non-compatible path flavours could lead to
Expand Down