Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-101112: Provide pattern overview for Path.glob #101223

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,9 @@ Pure paths provide the following methods and properties:

.. method:: PurePath.match(pattern, *, case_sensitive=None)

Match this path against the provided glob-style pattern. Return ``True``
if matching is successful, ``False`` otherwise.
Match this path against the provided glob-style pattern. The pattern uses
the same format as in :func:`Path.glob`. Return ``True`` if matching is
Copy link
Contributor

@barneygale barneygale Jun 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the same format as in :func:`Path.glob`. Return ``True`` if matching is
the same format as in :meth:`Path.glob`. Return ``True`` if matching is

successful, ``False`` otherwise.

If *pattern* is relative, the path can be either relative or absolute,
and matching is done from the right::
Expand Down Expand Up @@ -906,7 +907,7 @@ call fails (for example because the path doesn't exist).
>>> sorted(Path('.').glob('*/*.py'))
[PosixPath('docs/conf.py')]

Patterns are the same as for :mod:`fnmatch`, with the addition of "``**``"
Pattern segments are the same as for :mod:`fnmatch`, with the addition of "``**``"
which means "this directory and all subdirectories, recursively". In other
words, it enables recursive globbing::

Expand All @@ -917,6 +918,22 @@ call fails (for example because the path doesn't exist).
PosixPath('setup.py'),
PosixPath('test_pathlib.py')]

The following wildcards are available:

+------------+----------------------------------------------------------+
| Pattern | Meaning |
+============+==========================================================+
| ``**`` | matches any number of nested directories |
+------------+----------------------------------------------------------+
| ``*`` | matches any part of a file or directory name |
+------------+----------------------------------------------------------+
| ``?`` | matches any single character in a file or directory name |
+------------+----------------------------------------------------------+
| ``[seq]`` | matches any character in seq |
+------------+----------------------------------------------------------+
| ``[!seq]`` | matches any character not in seq |
+------------+----------------------------------------------------------+

By default, or when the *case_sensitive* keyword-only argument is set to
``None``, this method matches paths using platform-specific casing rules:
typically, case-sensitive on POSIX, and case-insensitive on Windows.
Expand Down Expand Up @@ -1331,8 +1348,7 @@ call fails (for example because the path doesn't exist).
.. method:: Path.rglob(pattern, *, case_sensitive=None, follow_symlinks=None)

Glob the given relative *pattern* recursively. This is like calling
:func:`Path.glob` with "``**/``" added in front of the *pattern*, where
*patterns* are the same as for :mod:`fnmatch`::
:func:`Path.glob` with "``**/``" added in front of the *pattern*::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:func:`Path.glob` with "``**/``" added in front of the *pattern*::
:meth:`~Path.glob` with "``**/``" added in front of the *pattern*::


>>> sorted(Path().rglob("*.py"))
[PosixPath('build/lib/pathlib.py'),
Expand Down