@@ -973,18 +973,16 @@ call fails (for example because the path doesn't exist).
973
973
[PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
974
974
>>> sorted(Path('.').glob('*/*.py'))
975
975
[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
-
981
976
>>> sorted(Path('.').glob('**/*.py'))
982
977
[PosixPath('build/lib/pathlib.py'),
983
978
PosixPath('docs/conf.py'),
984
979
PosixPath('pathlib.py'),
985
980
PosixPath('setup.py'),
986
981
PosixPath('test_pathlib.py')]
987
982
983
+ .. seealso ::
984
+ :ref: `pattern-language ` documentation.
985
+
988
986
.. note ::
989
987
Using the "``** ``" pattern in large directory trees may consume
990
988
an inordinate amount of time.
@@ -1020,6 +1018,24 @@ call fails (for example because the path doesn't exist).
1020
1018
future Python release, patterns with this ending will match both files
1021
1019
and directories. Add a trailing slash to match only directories.
1022
1020
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
+
1023
1039
.. method :: Path.group(*, follow_symlinks=True)
1024
1040
1025
1041
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).
1447
1463
strict mode, and no exception is raised in non-strict mode. In previous
1448
1464
versions, :exc: `RuntimeError ` is raised no matter the value of *strict *.
1449
1465
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
-
1485
1466
.. method :: Path.rmdir()
1486
1467
1487
1468
Remove this directory. The directory must be empty.
@@ -1608,6 +1589,33 @@ call fails (for example because the path doesn't exist).
1608
1589
.. versionchanged :: 3.10
1609
1590
The *newline * parameter was added.
1610
1591
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
+
1611
1619
Correspondence to tools in the :mod: `os ` module
1612
1620
-----------------------------------------------
1613
1621
0 commit comments