Skip to content

Commit 59224b8

Browse files
[3.12] GH-119054: Add "Reading directories" section to pathlib docs (GH-119956) (#120184)
Add a dedicated subsection for `Path.iterdir()`-related methods, specifically `iterdir()`, `glob()`, `rglob()` and `walk()`. (cherry picked from commit 14e1506) Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 0e7b4d2 commit 59224b8

File tree

1 file changed

+114
-107
lines changed

1 file changed

+114
-107
lines changed

Doc/library/pathlib.rst

Lines changed: 114 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,9 @@ bugs or failures in your application)::
789789
% (cls.__name__,))
790790
NotImplementedError: cannot instantiate 'WindowsPath' on your system
791791

792+
Some concrete path methods can raise an :exc:`OSError` if a system call fails
793+
(for example because the path doesn't exist).
794+
792795

793796
Querying file type and status
794797
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1040,71 +1043,32 @@ Reading and writing files
10401043
.. versionadded:: 3.5
10411044

10421045

1043-
Other methods
1044-
^^^^^^^^^^^^^
1045-
1046-
Many of these methods can raise an :exc:`OSError` if a system call fails (for
1047-
example because the path doesn't exist).
1048-
1049-
1050-
.. classmethod:: Path.cwd()
1051-
1052-
Return a new path object representing the current directory (as returned
1053-
by :func:`os.getcwd`)::
1054-
1055-
>>> Path.cwd()
1056-
PosixPath('/home/antoine/pathlib')
1057-
1058-
1059-
.. classmethod:: Path.home()
1060-
1061-
Return a new path object representing the user's home directory (as
1062-
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1063-
directory can't be resolved, :exc:`RuntimeError` is raised.
1064-
1065-
::
1066-
1067-
>>> Path.home()
1068-
PosixPath('/home/antoine')
1069-
1070-
.. versionadded:: 3.5
1046+
Reading directories
1047+
^^^^^^^^^^^^^^^^^^^
10711048

1049+
.. method:: Path.iterdir()
10721050

1073-
.. method:: Path.chmod(mode, *, follow_symlinks=True)
1074-
1075-
Change the file mode and permissions, like :func:`os.chmod`.
1076-
1077-
This method normally follows symlinks. Some Unix flavours support changing
1078-
permissions on the symlink itself; on these platforms you may add the
1079-
argument ``follow_symlinks=False``, or use :meth:`~Path.lchmod`.
1080-
1081-
::
1082-
1083-
>>> p = Path('setup.py')
1084-
>>> p.stat().st_mode
1085-
33277
1086-
>>> p.chmod(0o444)
1087-
>>> p.stat().st_mode
1088-
33060
1089-
1090-
.. versionchanged:: 3.10
1091-
The *follow_symlinks* parameter was added.
1092-
1093-
1094-
.. method:: Path.expanduser()
1095-
1096-
Return a new path with expanded ``~`` and ``~user`` constructs,
1097-
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1098-
resolved, :exc:`RuntimeError` is raised.
1099-
1100-
::
1051+
When the path points to a directory, yield path objects of the directory
1052+
contents::
11011053

1102-
>>> p = PosixPath('~/films/Monty Python')
1103-
>>> p.expanduser()
1104-
PosixPath('/home/eric/films/Monty Python')
1054+
>>> p = Path('docs')
1055+
>>> for child in p.iterdir(): child
1056+
...
1057+
PosixPath('docs/conf.py')
1058+
PosixPath('docs/_templates')
1059+
PosixPath('docs/make.bat')
1060+
PosixPath('docs/index.rst')
1061+
PosixPath('docs/_build')
1062+
PosixPath('docs/_static')
1063+
PosixPath('docs/Makefile')
11051064

1106-
.. versionadded:: 3.5
1065+
The children are yielded in arbitrary order, and the special entries
1066+
``'.'`` and ``'..'`` are not included. If a file is removed from or added
1067+
to the directory after creating the iterator, it is unspecified whether
1068+
a path object for that file is included.
11071069

1070+
If the path is not a directory or otherwise inaccessible, :exc:`OSError` is
1071+
raised.
11081072

11091073
.. method:: Path.glob(pattern, *, case_sensitive=None)
11101074

@@ -1150,32 +1114,33 @@ example because the path doesn't exist).
11501114
The *case_sensitive* parameter was added.
11511115

11521116

1153-
.. method:: Path.group()
1117+
.. method:: Path.rglob(pattern, *, case_sensitive=None)
11541118

1155-
Return the name of the group owning the file. :exc:`KeyError` is raised
1156-
if the file's gid isn't found in the system database.
1119+
Glob the given relative *pattern* recursively. This is like calling
1120+
:func:`Path.glob` with "``**/``" added in front of the *pattern*, where
1121+
*patterns* are the same as for :mod:`fnmatch`::
11571122

1123+
>>> sorted(Path().rglob("*.py"))
1124+
[PosixPath('build/lib/pathlib.py'),
1125+
PosixPath('docs/conf.py'),
1126+
PosixPath('pathlib.py'),
1127+
PosixPath('setup.py'),
1128+
PosixPath('test_pathlib.py')]
11581129

1159-
.. method:: Path.iterdir()
1130+
By default, or when the *case_sensitive* keyword-only argument is set to
1131+
``None``, this method matches paths using platform-specific casing rules:
1132+
typically, case-sensitive on POSIX, and case-insensitive on Windows.
1133+
Set *case_sensitive* to ``True`` or ``False`` to override this behaviour.
11601134

1161-
When the path points to a directory, yield path objects of the directory
1162-
contents::
1135+
.. audit-event:: pathlib.Path.rglob self,pattern pathlib.Path.rglob
11631136

1164-
>>> p = Path('docs')
1165-
>>> for child in p.iterdir(): child
1166-
...
1167-
PosixPath('docs/conf.py')
1168-
PosixPath('docs/_templates')
1169-
PosixPath('docs/make.bat')
1170-
PosixPath('docs/index.rst')
1171-
PosixPath('docs/_build')
1172-
PosixPath('docs/_static')
1173-
PosixPath('docs/Makefile')
1137+
.. versionchanged:: 3.11
1138+
Return only directories if *pattern* ends with a pathname components
1139+
separator (:data:`~os.sep` or :data:`~os.altsep`).
1140+
1141+
.. versionchanged:: 3.12
1142+
The *case_sensitive* parameter was added.
11741143

1175-
The children are yielded in arbitrary order, and the special entries
1176-
``'.'`` and ``'..'`` are not included. If a file is removed from or added
1177-
to the directory after creating the iterator, whether a path object for
1178-
that file be included is unspecified.
11791144

11801145
.. method:: Path.walk(top_down=True, on_error=None, follow_symlinks=False)
11811146

@@ -1272,6 +1237,75 @@ example because the path doesn't exist).
12721237

12731238
.. versionadded:: 3.12
12741239

1240+
1241+
Other methods
1242+
^^^^^^^^^^^^^
1243+
1244+
.. classmethod:: Path.cwd()
1245+
1246+
Return a new path object representing the current directory (as returned
1247+
by :func:`os.getcwd`)::
1248+
1249+
>>> Path.cwd()
1250+
PosixPath('/home/antoine/pathlib')
1251+
1252+
1253+
.. classmethod:: Path.home()
1254+
1255+
Return a new path object representing the user's home directory (as
1256+
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1257+
directory can't be resolved, :exc:`RuntimeError` is raised.
1258+
1259+
::
1260+
1261+
>>> Path.home()
1262+
PosixPath('/home/antoine')
1263+
1264+
.. versionadded:: 3.5
1265+
1266+
1267+
.. method:: Path.chmod(mode, *, follow_symlinks=True)
1268+
1269+
Change the file mode and permissions, like :func:`os.chmod`.
1270+
1271+
This method normally follows symlinks. Some Unix flavours support changing
1272+
permissions on the symlink itself; on these platforms you may add the
1273+
argument ``follow_symlinks=False``, or use :meth:`~Path.lchmod`.
1274+
1275+
::
1276+
1277+
>>> p = Path('setup.py')
1278+
>>> p.stat().st_mode
1279+
33277
1280+
>>> p.chmod(0o444)
1281+
>>> p.stat().st_mode
1282+
33060
1283+
1284+
.. versionchanged:: 3.10
1285+
The *follow_symlinks* parameter was added.
1286+
1287+
1288+
.. method:: Path.expanduser()
1289+
1290+
Return a new path with expanded ``~`` and ``~user`` constructs,
1291+
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1292+
resolved, :exc:`RuntimeError` is raised.
1293+
1294+
::
1295+
1296+
>>> p = PosixPath('~/films/Monty Python')
1297+
>>> p.expanduser()
1298+
PosixPath('/home/eric/films/Monty Python')
1299+
1300+
.. versionadded:: 3.5
1301+
1302+
1303+
.. method:: Path.group()
1304+
1305+
Return the name of the group owning the file. :exc:`KeyError` is raised
1306+
if the file's gid isn't found in the system database.
1307+
1308+
12751309
.. method:: Path.lchmod(mode)
12761310

12771311
Like :meth:`Path.chmod` but, if the path points to a symbolic link, the
@@ -1401,33 +1435,6 @@ example because the path doesn't exist).
14011435
.. versionchanged:: 3.6
14021436
The *strict* parameter was added (pre-3.6 behavior is strict).
14031437

1404-
.. method:: Path.rglob(pattern, *, case_sensitive=None)
1405-
1406-
Glob the given relative *pattern* recursively. This is like calling
1407-
:func:`Path.glob` with "``**/``" added in front of the *pattern*, where
1408-
*patterns* are the same as for :mod:`fnmatch`::
1409-
1410-
>>> sorted(Path().rglob("*.py"))
1411-
[PosixPath('build/lib/pathlib.py'),
1412-
PosixPath('docs/conf.py'),
1413-
PosixPath('pathlib.py'),
1414-
PosixPath('setup.py'),
1415-
PosixPath('test_pathlib.py')]
1416-
1417-
By default, or when the *case_sensitive* keyword-only argument is set to
1418-
``None``, this method matches paths using platform-specific casing rules:
1419-
typically, case-sensitive on POSIX, and case-insensitive on Windows.
1420-
Set *case_sensitive* to ``True`` or ``False`` to override this behaviour.
1421-
1422-
.. audit-event:: pathlib.Path.rglob self,pattern pathlib.Path.rglob
1423-
1424-
.. versionchanged:: 3.11
1425-
Return only directories if *pattern* ends with a pathname components
1426-
separator (:data:`~os.sep` or :data:`~os.altsep`).
1427-
1428-
.. versionchanged:: 3.12
1429-
The *case_sensitive* parameter was added.
1430-
14311438

14321439
.. method:: Path.rmdir()
14331440

0 commit comments

Comments
 (0)