8000 GH-119054: Add "Reading directories" section to pathlib docs (#119956) · python/cpython@14e1506 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14e1506

Browse files
GH-119054: Add "Reading directories" section to pathlib docs (#119956)
Add a dedicated subsection for `Path.iterdir()`-related methods, specifically `iterdir()`, `glob()`, `rglob()` and `walk()`. Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 5bdc87b commit 14e1506

File tree

1 file changed

+102
-95
lines changed

1 file changed

+102
-95
lines changed

Doc/library/pathlib.rst

Lines changed: 102 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,9 @@ bugs or failures in your application)::
820820
% (cls.__name__,))
821821
UnsupportedOperation: cannot instantiate 'WindowsPath' on your system
822822

823+
Some concrete path methods can raise an :exc:`OSError` if a system call fails
824+
(for example because the path doesn't exist).
825+
823826

824827
Parsing and generating URIs
825828
^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1151,69 +1154,32 @@ Reading and writing files
11511154
.. versionadded:: 3.5
11521155

11531156

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

1204-
.. method:: Path.expanduser()
1160+
.. method:: Path.iterdir()
12051161

1206-
Return a new path with expanded ``~`` and ``~user`` constructs,
1207-
as returned by :meth:`os.path.expanduser`. If a home directory can't be
1208-
resolved, :exc:`RuntimeError` is raised.
1162+
When the path points to a directory, yield path objects of the directory
1163+
contents::
12091164

1210-
::
1165+
>>> p = Path('docs')
1166+
>>> for child in p.iterdir(): child
1167+
...
1168+
PosixPath('docs/conf.py')
1169+
PosixPath('docs/_templates')
1170+
PosixPath('docs/make.bat')
1171+
PosixPath('docs/index.rst')
1172+
PosixPath('docs/_build')
1173+
PosixPath('docs/_static')
1174+
PosixPath('docs/Makefile')
12111175

1212-
>>> p = PosixPath('~/films/Monty Python')
1213-
>>> p.expanduser()
1214-
PosixPath('/home/eric/films/Monty Python')
1176+
The children are yielded in arbitrary order, and the special entries
1177+
``'.'`` and ``'..'`` are not included. If a file is removed from or added
1178+
to the directory after creating the iterator, it is unspecified whether
1179+
a path object for that file is included.
12151180

1216-
.. versionadded:: 3.5
1181+
If the path is not a directory or otherwise inaccessible, :exc:`OSError` is
1182+
raised.
12171183

12181184

12191185
.. method:: Path.glob(pattern, *, case_sensitive=None, recurse_symlinks=False)
@@ -1281,43 +1247,6 @@ example because the path doesn't exist).
12811247
The *pattern* parameter accepts a :term:`path-like object`.
12821248

12831249

1284-
.. method:: Path.group(*, follow_symlinks=True)
1285-
1286-
Return the name of the group owning the file. :exc:`KeyError` is raised
1287-
if the file's gid isn't found in the system database.
1288-
1289-
This method normally follows symlinks; to get the group of the symlink, add
1290-
the argument ``follow_symlinks=False``.
1291-
1292-
.. versionchanged:: 3.13
1293-
Raises :exc:`UnsupportedOperation` if the :mod:`grp` module is not
1294-
available. In previous versions, :exc:`NotImplementedError` was raised.
1295-
1296-
.. versionchanged:: 3.13
1297-
The *follow_symlinks* parameter was added.
1298-
1299-
1300-
.. method:: Path.iterdir()
1301-
1302-
When the path points to a directory, yield path objects of the directory
1303-
contents::
1304-
1305-
>>> p = Path('docs')
1306-
>>> for child in p.iterdir(): child
1307-
...
1308-
PosixPath('docs/conf.py')
1309-
PosixPath('docs/_templates')
1310-
PosixPath('docs/make.bat')
1311-
PosixPath('docs/index.rst')
1312-
PosixPath('docs/_build')
1313-
PosixPath('docs/_static')
1314-
PosixPath('docs/Makefile')
1315-
1316-
The children are yielded in arbitrary order, and the special entries
1317-
``'.'`` and ``'..'`` are not included. If a file is removed from or added
1318-
to the directory after creating the iterator, whether a path object for
1319-
that file be included is unspecified.
1320-
13211250
.. method:: Path.walk(top_down=True, on_error=None, follow_symlinks=False)
13221251

13231252
Generate the file names in a directory tree by walking the tree
@@ -1413,6 +1342,84 @@ example because the path doesn't exist).
14131342

14141343
.. versionadded:: 3.12
14151344

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

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

0 commit comments

Comments
 (0)
0