Skip to content

Commit db927ea

Browse files
committed
pythonGH-101112: Add "pattern language" section to pathlib docs
Explain the `match()` / `glob()` / `rglob()` pattern language in its own section. Move `rglob()` documentation under `glob()` and reduce duplicated text.
1 parent c7d59bd commit db927ea

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

Doc/library/pathlib.rst

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -973,18 +973,16 @@ call fails (for example because the path doesn't exist).
973973
[PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
974974
>>> sorted(Path('.').glob('*/*.py'))
975975
[PosixPath('docs/conf.py')]
976-
977-
Patterns are the same as for :mod:`fnmatch`, with the addition of "``**``"
978-
which means "this directory and all subdirectories, recursively". In other
979-
words, it enables recursive globbing::
980-
981976
>>> sorted(Path('.').glob('**/*.py'))
982977
[PosixPath('build/lib/pathlib.py'),
983978
PosixPath('docs/conf.py'),
984979
PosixPath('pathlib.py'),
985980
PosixPath('setup.py'),
986981
PosixPath('test_pathlib.py')]
987982

983+
.. seealso::
984+
:ref:`pattern-language` documentation.
985+
988986
.. note::
989987
Using the "``**``" pattern in large directory trees may consume
990988
an inordinate amount of time.
@@ -1020,6 +1018,24 @@ call fails (for example because the path doesn't exist).
10201018
future Python release, patterns with this ending will match both files
10211019
and directories. Add a trailing slash to match only directories.
10221020

1021+
1022+
.. method:: Path.rglob(pattern, *, case_sensitive=None, follow_symlinks=None)
1023+
1024+
Glob the given relative *pattern* recursively. This is exactly like
1025+
calling :func:`Path.glob` with "``**/``" added in front of the *pattern*.
1026+
1027+
.. seealso::
1028+
:ref:`pattern-language` and :meth:`Path.glob` documentation.
1029+
1030+
.. audit-event:: pathlib.Path.rglob self,pattern pathlib.Path.rglob
1031+
1032+
.. versionchanged:: 3.12
1033+
The *case_sensitive* parameter was added.
1034+
1035+
.. versionchanged:: 3.13
1036+
The *follow_symlinks* parameter was added.
1037+
1038+
10231039
.. method:: Path.group(*, follow_symlinks=True)
10241040

10251041
Return the name of the group owning the file. :exc:`KeyError` is raised
@@ -1447,41 +1463,6 @@ call fails (for example because the path doesn't exist).
14471463
strict mode, and no exception is raised in non-strict mode. In previous
14481464
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.
14491465

1450-
.. method:: Path.rglob(pattern, *, case_sensitive=None, follow_symlinks=None)
1451-
1452-
Glob the given relative *pattern* recursively. This is like calling
1453-
:func:`Path.glob` with "``**/``" added in front of the *pattern*, where
1454-
*patterns* are the same as for :mod:`fnmatch`::
1455-
1456-
>>> sorted(Path().rglob("*.py"))
1457-
[PosixPath('build/lib/pathlib.py'),
1458-
PosixPath('docs/conf.py'),
1459-
PosixPath('pathlib.py'),
1460-
PosixPath('setup.py'),
1461-
PosixPath('test_pathlib.py')]
1462-
1463-
By default, or when the *case_sensitive* keyword-only argument is set to
1464-
``None``, this method matches paths using platform-specific casing rules:
1465-
typically, case-sensitive on POSIX, and case-insensitive on Windows.
1466-
Set *case_sensitive* to ``True`` or ``False`` to override this behaviour.
1467-
1468-
By default, or when the *follow_symlinks* keyword-only argument is set to
1469-
``None``, this method follows symlinks except when expanding "``**``"
1470-
wildcards. Set *follow_symlinks* to ``True`` to always follow symlinks, or
1471-
``False`` to treat all symlinks as files.
1472-
1473-
.. audit-event:: pathlib.Path.rglob self,pattern pathlib.Path.rglob
1474-
1475-
.. versionchanged:: 3.11
1476-
Return only directories if *pattern* ends with a pathname components
1477-
separator (:data:`~os.sep` or :data:`~os.altsep`).
1478-
1479-
.. versionchanged:: 3.12
1480-
The *case_sensitive* parameter was added.
1481-
1482-
.. versionchanged:: 3.13
1483-
The *follow_symlinks* parameter was added.
1484-
14851466
.. method:: Path.rmdir()
14861467

14871468
Remove this directory. The directory must be empty.
@@ -1608,6 +1589,33 @@ call fails (for example because the path doesn't exist).
16081589
.. versionchanged:: 3.10
16091590
The *newline* parameter was added.
16101591

1592+
1593+
.. _pattern-language:
1594+
1595+
Pattern language
1596+
----------------
1597+
1598+
:meth:`PurePath.match`, :meth:`Path.glob` and :meth:`Path.rglob` accept
1599+
patterns with shell-style wildcards.
1600+
1601+
The "``**``" wildcard matches any number of file or directory segments. In
1602+
other words, it enabled recursive globbing. If "``**``" occurs in any position
1603+
other than a full pattern segment, :exc:`ValueError` is raised.
1604+
1605+
The "``*``" wildcard matches precisely one file or directory segment when it
1606+
occurs as a full pattern segment, or any number of non-separator characters
1607+
when it occurs elsewhere.
1608+
1609+
The "``?``" wildcard matches a single non-separator character. Character
1610+
ranges such as "``[seq]``" and "``[!seq]``" match a single character within or
1611+
without the range.
1612+
1613+
If the pattern ends with a path separator, only directories are matched.
1614+
1615+
For a literal match, wrap the meta-characters in brackets.
1616+
For example, ``"[?]"`` matches the character ``"?"``.
1617+
1618+
16111619
Correspondence to tools in the :mod:`os` module
16121620
-----------------------------------------------
16131621

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document :mod:`pathlib` pattern language in its own subsection.

0 commit comments

Comments
 (0)