@@ -1664,7 +1664,7 @@ the pattern to match only directories.
16641664
16651665
16661666Comparison to the :mod: `glob ` module
1667- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1667+ ------------------------------------
16681668
16691669The patterns accepted and results generated by :meth: `Path.glob ` and
16701670:meth: `Path.rglob ` differ slightly from those by the :mod: `glob ` module:
@@ -1682,27 +1682,57 @@ The patterns accepted and results generated by :meth:`Path.glob` and
168216825. The values returned from pathlib's ``path.glob() `` and ``path.rglob() ``
16831683 include the *path * as a prefix, unlike the results of
16841684 ``glob.glob(root_dir=path) ``.
1685- 6. ``bytes ``-based paths and :ref: `paths relative to directory descriptors
1686- <dir_fd>` are not supported by pathlib.
16871685
16881686
1689- Correspondence to tools in the :mod: `os ` module
1690- -----------------------------------------------
1687+ Comparison to the :mod: ` os ` and :mod: `os.path ` modules
1688+ ------------------------------------------------------
16911689
1692- Below is a table mapping various :mod: `os ` functions to their corresponding
1693- :class: `PurePath `/:class: `Path ` equivalent.
1690+ pathlib implements path operations using :class: `PurePath ` and :class: `Path `
1691+ objects, and so it's said to be *object-oriented *. On the other hand, the
1692+ :mod: `os ` and :mod: `os.path ` modules supply functions that work with low-level
1693+ ``str `` and ``bytes `` objects, which is a more *procedural * approach. Some
1694+ users consider the object-oriented style to be more readable.
16941695
1695- .. note ::
1696+ Many functions in :mod: `os ` and :mod: `os.path ` support ``bytes `` paths and
1697+ :ref: `paths relative to directory descriptors <dir_fd >`. These features aren't
1698+ available in pathlib.
1699+
1700+ Python's ``str `` and ``bytes `` types, and portions of the :mod: `os ` and
1701+ :mod: `os.path ` modules, are written in C and are very speedy. pathlib is
1702+ written in pure Python and is often slower, but rarely slow enough to matter.
1703+
1704+ pathlib's path normalization is slightly more opinionated and consistent than
1705+ :mod: `os.path `. For example, whereas :func: `os.path.abspath ` eliminates
1706+ "``.. ``" segments from a path, which may change its meaning if symlinks are
1707+ involved, :meth: `Path.absolute ` preserves these segments for greater safety.
1708+
1709+ pathlib's path normalization may render it unsuitable for some applications:
1710+
1711+ 1. pathlib normalizes ``Path("my_folder/") `` to ``Path("my_folder") ``, which
1712+ changes a path's meaning when supplied to various operating system APIs and
1713+ command-line utilities. Specifically, the absence of a trailing separator
1714+ may allow the path to be resolved as either a file or directory, rather
1715+ than a directory only.
1716+ 2. pathlib normalizes ``Path("./my_program") `` to ``Path("my_program") ``,
1717+ which changes a path's meaning when used as an executable search path, such
1718+ as in a shell or when spawning a child process. Specifically, the absence
1719+ of a separator in the path may force it to be looked up in :envvar: `PATH `
1720+ rather than the current directory.
16961721
1697- Not all pairs of functions/methods below are equivalent. Some of them,
1698- despite having some overlapping use-cases, have different semantics. They
1699- include :func: `os.path.abspath ` and :meth: `Path.absolute `,
1700- :func: `os.path.relpath ` and :meth: `PurePath.relative_to `.
1722+ As a consequence of these differences, pathlib is not a drop-in replacement
1723+ for :mod: `os.path `.
1724+
1725+
1726+ Corresponding tools
1727+ ^^^^^^^^^^^^^^^^^^^
1728+
1729+ Below is a table mapping various :mod: `os ` functions to their corresponding
1730+ :class: `PurePath `/:class: `Path ` equivalent.
17011731
17021732==================================== ==============================
17031733:mod: `os ` and :mod: `os.path ` :mod: `pathlib `
17041734==================================== ==============================
1705- :func: `os.path.abspath ` :meth: `Path.absolute ` [ # ]_
1735+ :func: `os.path.abspath ` :meth: `Path.absolute `
17061736:func: `os.path.realpath ` :meth: `Path.resolve `
17071737:func: `os.chmod ` :meth: `Path.chmod `
17081738:func: `os.mkdir ` :meth: `Path.mkdir `
@@ -1723,7 +1753,7 @@ Below is a table mapping various :mod:`os` functions to their corresponding
17231753:func: `os.link ` :meth: `Path.hardlink_to `
17241754:func: `os.symlink ` :meth: `Path.symlink_to `
17251755:func: `os.readlink ` :meth: `Path.readlink `
1726- :func: `os.path.relpath ` :meth: `PurePath.relative_to ` [ # ]_
1756+ :func: `os.path.relpath ` :meth: `PurePath.relative_to `
17271757:func: `os.stat ` :meth: `Path.stat `,
17281758 :meth: `Path.owner `,
17291759 :meth: `Path.group `
@@ -1735,8 +1765,3 @@ Below is a table mapping various :mod:`os` functions to their corresponding
17351765:func: `os.path.splitext ` :attr: `PurePath.stem ` and
17361766 :attr: `PurePath.suffix `
17371767==================================== ==============================
1738-
1739- .. rubric :: Footnotes
1740-
1741- .. [# ] :func: `os.path.abspath ` normalizes the resulting path, which may change its meaning in the presence of symlinks, while :meth: `Path.absolute ` does not.
1742- .. [# ] :meth: `PurePath.relative_to ` requires ``self `` to be the subpath of the argument, but :func: `os.path.relpath ` does not.
0 commit comments