Skip to content

Commit 8205c16

Browse files
authored
[3.12] GH-121462: pathlib docs: improve table of corresponding os/os.path functions (GH-121465) (#122360)
Re-order table of corresponding functions with the following priorities: 1. Pure functionality is at the top 2. `os.path` functions are shown before `os` functions 3. Similar functionality is kept together 4. Functionality follows docs order where possible Add a few missed correspondences: - `os.path.isjunction` and `Path.is_junction` - `os.path.ismount` and `Path.is_mount` - `os.lstat()` and `Path.lstat()` - `os.lchmod()` and `Path.lchmod()` Also add footnotes describing a few differences. (cherry picked from commit cbac8a3)
1 parent 0f83b8f commit 8205c16

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

Doc/library/pathlib.rst

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,51 +1527,54 @@ Correspondence to tools in the :mod:`os` module
15271527
Below is a table mapping various :mod:`os` functions to their corresponding
15281528
:class:`PurePath`/:class:`Path` equivalent.
15291529

1530-
.. note::
1531-
1532-
Not all pairs of functions/methods below are equivalent. Some of them,
1533-
despite having some overlapping use-cases, have different semantics. They
1534-
include :func:`os.path.abspath` and :meth:`Path.absolute`,
1535-
:func:`os.path.relpath` and :meth:`PurePath.relative_to`.
1536-
1537-
==================================== ==============================
1538-
:mod:`os` and :mod:`os.path` :mod:`pathlib`
1539-
==================================== ==============================
1540-
:func:`os.path.abspath` :meth:`Path.absolute` [#]_
1541-
:func:`os.path.realpath` :meth:`Path.resolve`
1542-
:func:`os.chmod` :meth:`Path.chmod`
1543-
:func:`os.mkdir` :meth:`Path.mkdir`
1544-
:func:`os.makedirs` :meth:`Path.mkdir`
1545-
:func:`os.rename` :meth:`Path.rename`
1546-
:func:`os.replace` :meth:`Path.replace`
1547-
:func:`os.rmdir` :meth:`Path.rmdir`
1548-
:func:`os.remove`, :func:`os.unlink` :meth:`Path.unlink`
1549-
:func:`os.getcwd` :func:`Path.cwd`
1550-
:func:`os.path.exists` :meth:`Path.exists`
1551-
:func:`os.path.expanduser` :meth:`Path.expanduser` and
1552-
:meth:`Path.home`
1553-
:func:`os.listdir` :meth:`Path.iterdir`
1554-
:func:`os.walk` :meth:`Path.walk`
1555-
:func:`os.path.isdir` :meth:`Path.is_dir`
1556-
:func:`os.path.isfile` :meth:`Path.is_file`
1557-
:func:`os.path.islink` :meth:`Path.is_symlink`
1558-
:func:`os.link` :meth:`Path.hardlink_to`
1559-
:func:`os.symlink` :meth:`Path.symlink_to`
1560-
:func:`os.readlink` :meth:`Path.readlink`
1561-
:func:`os.path.relpath` :meth:`PurePath.relative_to` [#]_
1562-
:func:`os.stat` :meth:`Path.stat`,
1563-
:meth:`Path.owner`,
1564-
:meth:`Path.group`
1565-
:func:`os.path.isabs` :meth:`PurePath.is_absolute`
1566-
:func:`os.path.join` :func:`PurePath.joinpath`
1567-
:func:`os.path.basename` :attr:`PurePath.name`
1568-
:func:`os.path.dirname` :attr:`PurePath.parent`
1569-
:func:`os.path.samefile` :meth:`Path.samefile`
1570-
:func:`os.path.splitext` :attr:`PurePath.stem` and
1571-
:attr:`PurePath.suffix`
1572-
==================================== ==============================
1530+
===================================== ==============================================
1531+
:mod:`os` and :mod:`os.path` :mod:`pathlib`
1532+
===================================== ==============================================
1533+
:func:`os.path.dirname` :attr:`PurePath.parent`
1534+
:func:`os.path.basename` :attr:`PurePath.name`
1535+
:func:`os.path.splitext` :attr:`PurePath.stem`, :attr:`PurePath.suffix`
1536+
:func:`os.path.join` :meth:`PurePath.joinpath`
1537+
:func:`os.path.isabs` :meth:`PurePath.is_absolute`
1538+
:func:`os.path.relpath` :meth:`PurePath.relative_to` [1]_
1539+
:func:`os.path.expanduser` :meth:`Path.expanduser` [2]_
1540+
:func:`os.path.realpath` :meth:`Path.resolve`
1541+
:func:`os.path.abspath` :meth:`Path.absolute` [3]_
1542+
:func:`os.path.exists` :meth:`Path.exists`
1543+
:func:`os.path.isfile` :meth:`Path.is_file`
1544+
:func:`os.path.isdir` :meth:`Path.is_dir`
1545+
:func:`os.path.islink` :meth:`Path.is_symlink`
1546+
:func:`os.path.isjunction` :meth:`Path.is_junction`
1547+
:func:`os.path.ismount` :meth:`Path.is_mount`
1548+
:func:`os.path.samefile` :meth:`Path.samefile`
1549+
:func:`os.getcwd` :meth:`Path.cwd`
1550+
:func:`os.stat` :meth:`Path.stat`
1551+
:func:`os.lstat` :meth:`Path.lstat`
1552+
:func:`os.listdir` :meth:`Path.iterdir`
1553+
:func:`os.walk` :meth:`Path.walk` [4]_
1554+
:func:`os.mkdir`, :func:`os.makedirs` :meth:`Path.mkdir`
1555+
:func:`os.link` :meth:`Path.hardlink_to`
1556+
:func:`os.symlink` :meth:`Path.symlink_to`
1557+
:func:`os.readlink` :meth:`Path.readlink`
1558+
:func:`os.rename` :meth:`Path.rename`
1559+
:func:`os.replace` :meth:`Path.replace`
1560+
:func:`os.remove`, :func:`os.unlink` :meth:`Path.unlink`
1561+
:func:`os.rmdir` :meth:`Path.rmdir`
1562+
:func:`os.chmod` :meth:`Path.chmod`
1563+
:func:`os.lchmod` :meth:`Path.lchmod`
1564+
===================================== ==============================================
15731565

15741566
.. rubric:: Footnotes
15751567

1576-
.. [#] :func:`os.path.abspath` normalizes the resulting path, which may change its meaning in the presence of symlinks, while :meth:`Path.absolute` does not.
1577-
.. [#] :meth:`PurePath.relative_to` requires ``self`` to be the subpath of the argument, but :func:`os.path.relpath` does not.
1568+
.. [1] :func:`os.path.relpath` calls :func:`~os.path.abspath` to make paths
1569+
absolute and remove "``..``" parts, whereas :meth:`PurePath.relative_to`
1570+
is a lexical operation that raises :exc:`ValueError` when its inputs'
1571+
anchors differ (e.g. if one path is absolute and the other relative.)
1572+
.. [2] :func:`os.path.expanduser` returns the path unchanged if the home
1573+
directory can't be resolved, whereas :meth:`Path.expanduser` raises
1574+
:exc:`RuntimeError`.
1575+
.. [3] :func:`os.path.abspath` removes "``..``" components without resolving
1576+
symlinks, which may change the meaning of the path, whereas
1577+
:meth:`Path.absolute` leaves any "``..``" components in the path.
1578+
.. [4] :func:`os.walk` always follows symlinks when categorizing paths into
1579+
*dirnames* and *filenames*, whereas :meth:`Path.walk` categorizes all
1580+
symlinks into *filenames* when *follow_symlinks* is false (the default.)

0 commit comments

Comments
 (0)