Skip to content

Commit d4566c0

Browse files
[3.13] GH-119054: Add "Reading directories" section to pathlib docs (GH-119956) (#120183)
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 <jelle.zijlstra@gmail.com>
1 parent 517733c commit d4566c0

File tree

1 file changed

+103
-96
lines changed

1 file changed

+103
-96
lines changed

Doc/library/pathlib.rst

+103-96
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,9 @@ bugs or failures in your application)::
807807
% (cls.__name__,))
808808
UnsupportedOperation: cannot instantiate 'WindowsPath' on your system
809809

810+
Some concrete path methods can raise an :exc:`OSError` if a system call fails
811+
(for example because the path doesn't exist).
812+
810813

811814
Parsing and generating URIs
812815
^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1134,70 +1137,32 @@ Reading and writing files
11341137
.. versionadded:: 3.5
11351138

11361139

1137-
Other methods
1138-
^^^^^^^^^^^^^
1139-
1140-
Some of these methods can raise an :exc:`OSError` if a system call fails (for
1141-
example because the path doesn't exist).
1142-
1143-
.. classmethod:: Path.cwd()
1144-
1145-
Return a new path object representing the current directory (as returned
1146-
by :func:`os.getcwd`)::
1147-
1148-
>>> Path.cwd()
1149-
PosixPath('/home/antoine/pathlib')
1150-
1151-
1152-
.. classmethod:: Path.home()
1153-
1154-
Return a new path object representing the user's home directory (as
1155-
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1156-
directory can't be resolved, :exc:`RuntimeError` is raised.
1157-
1158-
::
1159-
1160-
>>> Path.home()
1161-
PosixPath('/home/antoine')
1162-
1163-
.. versionadded:: 3.5
1164-
1165-
1166-
.. method:: Path.chmod(mode, *, follow_symlinks=True)
1167-
1168-
Change the file mode and permissions, like :func:`os.chmod`.
1169-
1170-
This method normally follows symlinks. Some Unix flavours support changing
1171-
permissions on the symlink itself; on these platforms you may add the
1172-
argument ``follow_symlinks=False``, or use :meth:`~Path.lchmod`.
1173-
1174-
::
1175-
1176-
>>> p = Path('setup.py')
1177-
>>> p.stat().st_mode
1178-
33277
1179-
>>> p.chmod(0o444)
1180-
>>> p.stat().st_mode
1181-
33060
1182-
1183-
.. versionchanged:: 3.10
1184-
The *follow_symlinks* parameter was added.
1185-
1186-
1187-
.. method:: Path.expanduser()
1140+
Reading directories
1141+
^^^^^^^^^^^^^^^^^^^
11881142

1189-
Return a new path with expanded ``~`` and ``~user`` constructs,
1190-
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1191-
resolved, :exc:`RuntimeError` is raised.
1143+
.. method:: Path.iterdir()
11921144

1193-
::
1145+
When the path points to a directory, yield path objects of the directory
1146+
contents::
11941147

1195-
>>> p = PosixPath('~/films/Monty Python')
1196-
>>> p.expanduser()
1197-
PosixPath('/home/eric/films/Monty Python')
1148+
>>> p = Path('docs')
1149+
>>> for child in p.iterdir(): child
1150+
...
1151+
PosixPath('docs/conf.py')
1152+
PosixPath('docs/_templates')
1153+
PosixPath('docs/make.bat')
1154+
PosixPath('docs/index.rst')
1155+
PosixPath('docs/_build')
1156+
PosixPath('docs/_static')
1157+
PosixPath('docs/Makefile')
11981158

1199-
.. versionadded:: 3.5
1159+
The children are yielded in arbitrary order, and the special entries
1160+
``'.'`` and ``'..'`` are not included. If a file is removed from or added
1161+
to the directory after creating the iterator, it is unspecified whether
1162+
a path object for that file is included.
12001163

1164+
If the path is not a directory or otherwise inaccessible, :exc:`OSError` is
1165+
raised.
12011166

12021167
.. method:: Path.glob(pattern, *, case_sensitive=None, recurse_symlinks=False)
12031168

@@ -1264,43 +1229,6 @@ example because the path doesn't exist).
12641229
The *pattern* parameter accepts a :term:`path-like object`.
12651230

12661231

1267-
.. method:: Path.group(*, follow_symlinks=True)
1268-
1269-
Return the name of the group owning the file. :exc:`KeyError` is raised
1270-
if the file's gid isn't found in the system database.
1271-
1272-
This method normally follows symlinks; to get the group of the symlink, add
1273-
the argument ``follow_symlinks=False``.
1274-
1275-
.. versionchanged:: 3.13
1276-
Raises :exc:`UnsupportedOperation` if the :mod:`grp` module is not
1277-
available. In previous versions, :exc:`NotImplementedError` was raised.
1278-
1279-
.. versionchanged:: 3.13
1280-
The *follow_symlinks* parameter was added.
1281-
1282-
1283-
.. method:: Path.iterdir()
1284-
1285-
When the path points to a directory, yield path objects of the directory
1286-
contents::
1287-
1288-
>>> p = Path('docs')
1289-
>>> for child in p.iterdir(): child
1290-
...
1291-
PosixPath('docs/conf.py')
1292-
PosixPath('docs/_templates')
1293-
PosixPath('docs/make.bat')
1294-
PosixPath('docs/index.rst')
1295-
PosixPath('docs/_build')
1296-
PosixPath('docs/_static')
1297-
PosixPath('docs/Makefile')
1298-
1299-
The children are yielded in arbitrary order, and the special entries
1300-
``'.'`` and ``'..'`` are not included. If a file is removed from or added
1301-
to the directory after creating the iterator, whether a path object for
1302-
that file be included is unspecified.
1303-
13041232
.. method:: Path.walk(top_down=True, on_error=None, follow_symlinks=False)
13051233

13061234
Generate the file names in a directory tree by walking the tree
@@ -1396,6 +1324,85 @@ example because the path doesn't exist).
13961324

13971325
.. versionadded:: 3.12
13981326

1327+
1328+
Other methods
1329+
^^^^^^^^^^^^^
1330+
1331+
.. classmethod:: Path.cwd()
1332+
1333+
Return a new path object representing the current directory (as returned
1334+
by :func:`os.getcwd`)::
1335+
1336+
>>> Path.cwd()
1337+
PosixPath('/home/antoine/pathlib')
1338+
1339+
1340+
.. classmethod:: Path.home()
1341+
1342+
Return a new path object representing the user's home directory (as
1343+
returned by :func:`os.path.expanduser` with ``~`` construct). If the home
1344+
directory can't be resolved, :exc:`RuntimeError` is raised.
1345+
1346+
::
1347+
1348+
>>> Path.home()
1349+
PosixPath('/home/antoine')
1350+
1351+
.. versionadded:: 3.5
1352+
1353+
1354+
.. method:: Path.chmod(mode, *, follow_symlinks=True)
1355+
1356+
Change the file mode and permissions, like :func:`os.chmod`.
1357+
1358+
This method normally follows symlinks. Some Unix flavours support changing
1359+
permissions on the symlink itself; on these platforms you may add the
1360+
argument ``follow_symlinks=False``, or use :meth:`~Path.lchmod`.
1361+
1362+
::
1363+
1364+
>>> p = Path('setup.py')
1365+
>>> p.stat().st_mode
1366+
33277
1367+
>>> p.chmod(0o444)
1368+
>>> p.stat().st_mode
1369+
33060
1370+
1371+
.. versionchanged:: 3.10
1372+
The *follow_symlinks* parameter was added.
1373+
1374+
1375+
.. method:: Path.expanduser()
1376+
1377+
Return a new path with expanded ``~`` and ``~user`` constructs,
1378+
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1379+
resolved, :exc:`RuntimeError` is raised.
1380+
1381+
::
1382+
1383+
>>> p = PosixPath('~/films/Monty Python')
1384+
>>> p.expanduser()
1385+
PosixPath('/home/eric/films/Monty Python')
1386+
1387+
.. versionadded:: 3.5
1388+
1389+
1390+
.. method:: Path.group(*, follow_symlinks=True)
1391+
1392+
Return the name of the group owning the file. :exc:`KeyError` is raised
1393+
if the file's gid isn't found in the system database.
1394+
1395+
This method normally follows symlinks; to get the group of the symlink, add
1396+
the argument ``follow_symlinks=False``.
1397+
1398+
.. versionchanged:: 3.13
1399+
Raises :exc:`UnsupportedOperation` if the :mod:`grp` module is not
1400+
available. In previous versions, :exc:`NotImplementedError` was raised.
1401+
1402+
.. versionchanged:: 3.13
1403+
The *follow_symlinks* parameter was added.
1404+
1405+
13991406
.. method:: Path.lchmod(mode)
14001407

14011408
Like :meth:`Path.chmod` but, if the path points to a symbolic link, the

0 commit comments

Comments
 (0)