Skip to content

Commit b2623f7

Browse files
committed
[3.12] pythonGH-119054: Add "Expanding and resolving paths" section to pathlib docs. (pythonGH-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 6a8cb77 commit b2623f7

File tree

1 file changed

+93
-94
lines changed

1 file changed

+93
-94
lines changed

Doc/library/pathlib.rst

+93-94
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,99 @@ Some concrete path methods can raise an :exc:`OSError` if a system call fails
793793
(for example because the path doesn't exist).
794794

795795

796+
Expanding and resolving paths
797+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
798+
799+
.. classmethod:: Path.home()
800+
801+
Return a new path object representing the user's home directory (as
802+
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
803+
directory can't be resolved, :exc:`RuntimeError` is raised.
804+
805+
::
806+
807+
>>> Path.home()
808+
PosixPath('/home/antoine')
809+
810+
.. versionadded:: 3.5
811+
812+
813+
.. method:: Path.expanduser()
814+
815+
Return a new path with expanded ``~`` and ``~user`` constructs,
816+
as returned by :meth:`os.path.expanduser`. If a home directory can't be
817+
resolved, :exc:`RuntimeError` is raised.
818+
819+
::
820+
821+
>>> p = PosixPath('~/films/Monty Python')
822+
>>> p.expanduser()
823+
PosixPath('/home/eric/films/Monty Python')
824+
825+
.. versionadded:: 3.5
826+
827+
828+
.. classmethod:: Path.cwd()
829+
830+
Return a new path object representing the current directory (as returned
831+
by :func:`os.getcwd`)::
832+
833+
>>> Path.cwd()
834+
PosixPath('/home/antoine/pathlib')
835+
836+
837+
.. method:: Path.absolute()
838+
839+
Make the path absolute, without normalization or resolving symlinks.
840+
Returns a new path object::
841+
842+
>>> p = Path('tests')
843+
>>> p
844+
PosixPath('tests')
845+
>>> p.absolute()
846+
PosixPath('/home/antoine/pathlib/tests')
847+
848+
849+
.. method:: Path.resolve(strict=False)
850+
851+
Make the path absolute, resolving any symlinks. A new path object is
852+
returned::
853+
854+
>>> p = Path()
855+
>>> p
856+
PosixPath('.')
857+
>>> p.resolve()
858+
PosixPath('/home/antoine/pathlib')
859+
860+
"``..``" components are also eliminated (this is the only method to do so)::
861+
862+
>>> p = Path('docs/../setup.py')
863+
>>> p.resolve()
864+
PosixPath('/home/antoine/pathlib/setup.py')
865+
866+
If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError`
867+
is raised. If *strict* is ``False``, the path is resolved as far as possible
868+
and any remainder is appended without checking whether it exists. If an
869+
infinite loop is encountered along the resolution path, :exc:`RuntimeError`
870+
is raised.
871+
872+
.. versionchanged:: 3.6
873+
The *strict* parameter was added (pre-3.6 behavior is strict).
874+
875+
876+
.. method:: Path.readlink()
877+
878+
Return the path to which the symbolic link points (as returned by
879+
:func:`os.readlink`)::
880+
881+
>>> p = Path('mylink')
882+
>>> p.symlink_to('setup.py')
883+
>>> p.readlink()
884+
PosixPath('setup.py')
885+
886+
.. versionadded:: 3.9
887+
888+
796889
Querying file type and status
797890
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
798891

@@ -1422,100 +1515,6 @@ Ownership and permissions
14221515
symbolic link's mode is changed rather than its target's.
14231516

14241517

1425-
Other methods
1426-
^^^^^^^^^^^^^
1427-
1428-
.. classmethod:: Path.cwd()
1429-
1430-
Return a new path object representing the current directory (as returned
1431-
by :func:`os.getcwd`)::
1432-
1433-
>>> Path.cwd()
1434-
PosixPath('/home/antoine/pathlib')
1435-
1436-
1437-
.. classmethod:: Path.home()
1438-
1439-
Return a new path object representing the user's home directory (as
1440-
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1441-
directory can't be resolved, :exc:`RuntimeError` is raised.
1442-
1443-
::
1444-
1445-
>>> Path.home()
1446-
PosixPath('/home/antoine')
1447-
1448-
.. versionadded:: 3.5
1449-
1450-
1451-
.. method:: Path.expanduser()
1452-
1453-
Return a new path with expanded ``~`` and ``~user`` constructs,
1454-
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1455-
resolved, :exc:`RuntimeError` is raised.
1456-
1457-
::
1458-
1459-
>>> p = PosixPath('~/films/Monty Python')
1460-
>>> p.expanduser()
1461-
PosixPath('/home/eric/films/Monty Python')
1462-
1463-
.. versionadded:: 3.5
1464-
1465-
1466-
.. method:: Path.readlink()
1467-
1468-
Return the path to which the symbolic link points (as returned by
1469-
:func:`os.readlink`)::
1470-
1471-
>>> p = Path('mylink')
1472-
>>> p.symlink_to('setup.py')
1473-
>>> p.readlink()
1474-
PosixPath('setup.py')
1475-
1476-
.. versionadded:: 3.9
1477-
1478-
1479-
.. method:: Path.absolute()
1480-
1481-
Make the path absolute, without normalization or resolving symlinks.
1482-
Returns a new path object::
1483-
1484-
>>> p = Path('tests')
1485-
>>> p
1486-
PosixPath('tests')
1487-
>>> p.absolute()
1488-
PosixPath('/home/antoine/pathlib/tests')
1489-
1490-
1491-
.. method:: Path.resolve(strict=False)
1492-
1493-
Make the path absolute, resolving any symlinks. A new path object is
1494-
returned::
1495-
1496-
>>> p = Path()
1497-
>>> p
1498-
PosixPath('.')
1499-
>>> p.resolve()
1500-
PosixPath('/home/antoine/pathlib')
1501-
1502-
"``..``" components are also eliminated (this is the only method to do so)::
1503-
1504-
>>> p = Path('docs/../setup.py')
1505-
>>> p.resolve()
1506-
PosixPath('/home/antoine/pathlib/setup.py')
1507-
1508-
If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError`
1509-
is raised. If *strict* is ``False``, the path is resolved as far as possible
1510-
and any remainder is appended without checking whether it exists. If an
1511-
infinite loop is encountered along the resolution path, :exc:`RuntimeError`
1512-
is raised.
1513-
1514-
.. versionchanged:: 3.6
1515-
The *strict* parameter was added (pre-3.6 behavior is strict).
1516-
1517-
1518-
15191518
Correspondence to tools in the :mod:`os` module
15201519
-----------------------------------------------
15211520

0 commit comments

Comments
 (0)