Skip to content

Commit c70f112

Browse files
[3.13] GH-119054: Add "Expanding and resolving paths" section to pathlib docs. (GH-120970) (#121155)
GH-119054: Add "Expanding and resolving paths" section to pathlib docs. (GH-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. (cherry picked from commit d6d8707) Co-authored-by: Barney Gale <[email protected]>
1 parent d96a52e commit c70f112

File tree

1 file changed

+101
-101
lines changed

1 file changed

+101
-101
lines changed

Doc/library/pathlib.rst

Lines changed: 101 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,107 @@ conforming to :rfc:`8089`.
872872
it strictly impure.
873873

874874

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

@@ -1537,107 +1638,6 @@ Permissions and ownership
15371638
symbolic link's mode is changed rather than its target's.
15381639

15391640

1540-
Other methods
1541-
^^^^^^^^^^^^^
1542-
1543-
.. classmethod:: Path.cwd()
1544-
1545-
Return a new path object representing the current directory (as returned
1546-
by :func:`os.getcwd`)::
1547-
1548-
>>> Path.cwd()
1549-
PosixPath('/home/antoine/pathlib')
1550-
1551-
1552-
.. classmethod:: Path.home()
1553-
1554-
Return a new path object representing the user's home directory (as
1555-
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1556-
directory can't be resolved, :exc:`RuntimeError` is raised.
1557-
1558-
::
1559-
1560-
>>> Path.home()
1561-
PosixPath('/home/antoine')
1562-
1563-
.. versionadded:: 3.5
1564-
1565-
1566-
.. method:: Path.expanduser()
1567-
1568-
Return a new path with expanded ``~`` and ``~user`` constructs,
1569-
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1570-
resolved, :exc:`RuntimeError` is raised.
1571-
1572-
::
1573-
1574-
>>> p = PosixPath('~/films/Monty Python')
1575-
>>> p.expanduser()
1576-
PosixPath('/home/eric/films/Monty Python')
1577-
1578-
.. versionadded:: 3.5
1579-
1580-
1581-
.. method:: Path.readlink()
1582-
1583-
Return the path to which the symbolic link points (as returned by
1584-
:func:`os.readlink`)::
1585-
1586-
>>> p = Path('mylink')
1587-
>>> p.symlink_to('setup.py')
1588-
>>> p.readlink()
1589-
PosixPath('setup.py')
1590-
1591-
.. versionadded:: 3.9
1592-
1593-
.. versionchanged:: 3.13
1594-
Raises :exc:`UnsupportedOperation` if :func:`os.readlink` is not
1595-
available. In previous versions, :exc:`NotImplementedError` was raised.
1596-
1597-
1598-
.. method:: Path.absolute()
1599-
1600-
Make the path absolute, without normalization or resolving symlinks.
1601-
Returns a new path object::
1602-
1603-
>>> p = Path('tests')
1604-
>>> p
1605-
PosixPath('tests')
1606-
>>> p.absolute()
1607-
PosixPath('/home/antoine/pathlib/tests')
1608-
1609-
1610-
.. method:: Path.resolve(strict=False)
1611-
1612-
Make the path absolute, resolving any symlinks. A new path object is
1613-
returned::
1614-
1615-
>>> p = Path()
1616-
>>> p
1617-
PosixPath('.')
1618-
>>> p.resolve()
1619-
PosixPath('/home/antoine/pathlib')
1620-
1621-
"``..``" components are also eliminated (this is the only method to do so)::
1622-
1623-
>>> p = Path('docs/../setup.py')
1624-
>>> p.resolve()
1625-
PosixPath('/home/antoine/pathlib/setup.py')
1626-
1627-
If a path doesn't exist or a symlink loop is encountered, and *strict* is
1628-
``True``, :exc:`OSError` is raised. If *strict* is ``False``, the path is
1629-
resolved as far as possible and any remainder is appended without checking
1630-
whether it exists.
1631-
1632-
.. versionchanged:: 3.6
1633-
The *strict* parameter was added (pre-3.6 behavior is strict).
1634-
1635-
.. versionchanged:: 3.13
1636-
Symlink loops are treated like other errors: :exc:`OSError` is raised in
1637-
strict mode, and no exception is raised in non-strict mode. In previous
1638-
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.
1639-
1640-
16411641
.. _pathlib-pattern-language:
16421642

16431643
Pattern language

0 commit comments

Comments
 (0)