Skip to content

Commit d6d8707

Browse files
authored
GH-119054: Add "Expanding and resolving paths" section to pathlib docs. (#120970)
Add dedicated subsection for `home()`, `expanduser()`, `cwd()`, `absolute()`, `resolve()` and `readlink()`. The position of this section keeps all the `Path` constructors (`Path()`, `Path.from_uri()`, `Path.home()` and `Path.cwd()`) near the top. Within the section, closely related methods are kept adjacent. Specifically: -.`home()` and `expanduser()` (the former calls the latter) - `cwd()` and `absolute()` (the former calls the latter) - `absolute()` and `resolve()` (both make paths absolute) - `resolve()` and `readlink()` (both read symlink targets) - Ditto `cwd()` and `absolute()` - Ditto `absolute()` and `resolve()` The "Other methods" section is removed.
1 parent 2cb84b1 commit d6d8707

File tree

1 file changed

+101
-101
lines changed

1 file changed

+101
-101
lines changed

Doc/library/pathlib.rst

+101-101
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,107 @@ conforming to :rfc:`8089`.
885885
it strictly impure.
886886

887887

888+
Expanding and resolving paths
889+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
890+
891+
.. classmethod:: Path.home()
892+
893+
Return a new path object representing the user's home directory (as
894+
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
895+
directory can't be resolved, :exc:`RuntimeError` is raised.
896+
897+
::
898+
899+
>>> Path.home()
900+
PosixPath('/home/antoine')
901+
902+
.. versionadded:: 3.5
903+
904+
905+
.. method:: Path.expanduser()
906+
907+
Return a new path with expanded ``~`` and ``~user`` constructs,
908+
as returned by :meth:`os.path.expanduser`. If a home directory can't be
909+
resolved, :exc:`RuntimeError` is raised.
910+
911+
::
912+
913+
>>> p = PosixPath('~/films/Monty Python')
914+
>>> p.expanduser()
915+
PosixPath('/home/eric/films/Monty Python')
916+
917+
.. versionadded:: 3.5
918+
919+
920+
.. classmethod:: Path.cwd()
921+
922+
Return a new path object representing the current directory (as returned
923+
by :func:`os.getcwd`)::
924+
925+
>>> Path.cwd()
926+
PosixPath('/home/antoine/pathlib')
927+
928+
929+
.. method:: Path.absolute()
930+
931+
Make the path absolute, without normalization or resolving symlinks.
932+
Returns a new path object::
933+
934+
>>> p = Path('tests')
935+
>>> p
936+
PosixPath('tests')
937+
>>> p.absolute()
938+
PosixPath('/home/antoine/pathlib/tests')
939+
940+
941+
.. method:: Path.resolve(strict=False)
942+
943+
Make the path absolute, resolving any symlinks. A new path object is
944+
returned::
945+
946+
>>> p = Path()
947+
>>> p
948+
PosixPath('.')
949+
>>> p.resolve()
950+
PosixPath('/home/antoine/pathlib')
951+
952+
"``..``" components are also eliminated (this is the only method to do so)::
953+
954+
>>> p = Path('docs/../setup.py')
955+
>>> p.resolve()
956+
PosixPath('/home/antoine/pathlib/setup.py')
957+
958+
If a path doesn't exist or a symlink loop is encountered, and *strict* is
959+
``True``, :exc:`OSError` is raised. If *strict* is ``False``, the path is
960+
resolved as far as possible and any remainder is appended without checking
961+
whether it exists.
962+
963+
.. versionchanged:: 3.6
964+
The *strict* parameter was added (pre-3.6 behavior is strict).
965+
966+
.. versionchanged:: 3.13
967+
Symlink loops are treated like other errors: :exc:`OSError` is raised in
968+
strict mode, and no exception is raised in non-strict mode. In previous
969+
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.
970+
971+
972+
.. method:: Path.readlink()
973+
974+
Return the path to which the symbolic link points (as returned by
975+
:func:`os.readlink`)::
976+
977+
>>> p = Path('mylink')
978+
>>> p.symlink_to('setup.py')
979+
>>> p.readlink()
980+
PosixPath('setup.py')
981+
982+
.. versionadded:: 3.9
983+
984+
.. versionchanged:: 3.13
985+
Raises :exc:`UnsupportedOperation` if :func:`os.readlink` is not
986+
available. In previous versions, :exc:`NotImplementedError` was raised.
987+
988+
888989
Querying file type and status
889990
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
890991

@@ -1605,107 +1706,6 @@ Permissions and ownership
16051706
symbolic link's mode is changed rather than its target's.
16061707

16071708

1608-
Other methods
1609-
^^^^^^^^^^^^^
1610-
1611-
.. classmethod:: Path.cwd()
1612-
1613-
Return a new path object representing the current directory (as returned
1614-
by :func:`os.getcwd`)::
1615-
1616-
>>> Path.cwd()
1617-
PosixPath('/home/antoine/pathlib')
1618-
1619-
1620-
.. classmethod:: Path.home()
1621-
1622-
Return a new path object representing the user's home directory (as
1623-
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1624-
directory can't be resolved, :exc:`RuntimeError` is raised.
1625-
1626-
::
1627-
1628-
>>> Path.home()
1629-
PosixPath('/home/antoine')
1630-
1631-
.. versionadded:: 3.5
1632-
1633-
1634-
.. method:: Path.expanduser()
1635-
1636-
Return a new path with expanded ``~`` and ``~user`` constructs,
1637-
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1638-
resolved, :exc:`RuntimeError` is raised.
1639-
1640-
::
1641-
1642-
>>> p = PosixPath('~/films/Monty Python')
1643-
>>> p.expanduser()
1644-
PosixPath('/home/eric/films/Monty Python')
1645-
1646-
.. versionadded:: 3.5
1647-
1648-
1649-
.. method:: Path.readlink()
1650-
1651-
Return the path to which the symbolic link points (as returned by
1652-
:func:`os.readlink`)::
1653-
1654-
>>> p = Path('mylink')
1655-
>>> p.symlink_to('setup.py')
1656-
>>> p.readlink()
1657-
PosixPath('setup.py')
1658-
1659-
.. versionadded:: 3.9
1660-
1661-
.. versionchanged:: 3.13
1662-
Raises :exc:`UnsupportedOperation` if :func:`os.readlink` is not
1663-
available. In previous versions, :exc:`NotImplementedError` was raised.
1664-
1665-
1666-
.. method:: Path.absolute()
1667-
1668-
Make the path absolute, without normalization or resolving symlinks.
1669-
Returns a new path object::
1670-
1671-
>>> p = Path('tests')
1672-
>>> p
1673-
PosixPath('tests')
1674-
>>> p.absolute()
1675-
PosixPath('/home/antoine/pathlib/tests')
1676-
1677-
1678-
.. method:: Path.resolve(strict=False)
1679-
1680-
Make the path absolute, resolving any symlinks. A new path object is
1681-
returned::
1682-
1683-
>>> p = Path()
1684-
>>> p
1685-
PosixPath('.')
1686-
>>> p.resolve()
1687-
PosixPath('/home/antoine/pathlib')
1688-
1689-
"``..``" components are also eliminated (this is the only method to do so)::
1690-
1691-
>>> p = Path('docs/../setup.py')
1692-
>>> p.resolve()
1693-
PosixPath('/home/antoine/pathlib/setup.py')
1694-
1695-
If a path doesn't exist or a symlink loop is encountered, and *strict* is
1696-
``True``, :exc:`OSError` is raised. If *strict* is ``False``, the path is
1697-
resolved as far as possible and any remainder is appended without checking
1698-
whether it exists.
1699-
1700-
.. versionchanged:: 3.6
1701-
The *strict* parameter was added (pre-3.6 behavior is strict).
1702-
1703-
.. versionchanged:: 3.13
1704-
Symlink loops are treated like other errors: :exc:`OSError` is raised in
1705-
strict mode, and no exception is raised in non-strict mode. In previous
1706-
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.
1707-
1708-
17091709
.. _pathlib-pattern-language:
17101710

17111711
Pattern language

0 commit comments

Comments
 (0)