@@ -1664,7 +1664,7 @@ the pattern to match only directories.
1664
1664
1665
1665
1666
1666
Comparison to the :mod: `glob ` module
1667
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1667
+ ------------------------------------
1668
1668
1669
1669
The patterns accepted and results generated by :meth: `Path.glob ` and
1670
1670
: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
1682
1682
5. The values returned from pathlib's ``path.glob() `` and ``path.rglob() ``
1683
1683
include the *path * as a prefix, unlike the results of
1684
1684
``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.
1687
1685
1688
1686
1689
- Correspondence to tools in the :mod: `os ` module
1690
- -----------------------------------------------
1687
+ Comparison to the :mod: ` os ` and :mod: `os.path ` modules
1688
+ ------------------------------------------------------
1691
1689
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.
1694
1695
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.
1696
1721
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.
1701
1731
1702
1732
==================================== ==============================
1703
1733
:mod: `os ` and :mod: `os.path ` :mod: `pathlib `
1704
1734
==================================== ==============================
1705
- :func: `os.path.abspath ` :meth: `Path.absolute ` [ # ]_
1735
+ :func: `os.path.abspath ` :meth: `Path.absolute `
1706
1736
:func: `os.path.realpath ` :meth: `Path.resolve `
1707
1737
:func: `os.chmod ` :meth: `Path.chmod `
1708
1738
:func: `os.mkdir ` :meth: `Path.mkdir `
@@ -1723,7 +1753,7 @@ Below is a table mapping various :mod:`os` functions to their corresponding
1723
1753
:func: `os.link ` :meth: `Path.hardlink_to `
1724
1754
:func: `os.symlink ` :meth: `Path.symlink_to `
1725
1755
:func: `os.readlink ` :meth: `Path.readlink `
1726
- :func: `os.path.relpath ` :meth: `PurePath.relative_to ` [ # ]_
1756
+ :func: `os.path.relpath ` :meth: `PurePath.relative_to `
1727
1757
:func: `os.stat ` :meth: `Path.stat `,
1728
1758
:meth: `Path.owner `,
1729
1759
:meth: `Path.group `
@@ -1735,8 +1765,3 @@ Below is a table mapping various :mod:`os` functions to their corresponding
1735
1765
:func: `os.path.splitext ` :attr: `PurePath.stem ` and
1736
1766
:attr: `PurePath.suffix `
1737
1767
==================================== ==============================
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