You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support recursive wildcards in pathlib.PurePath.match() #73435
** is supported not just as a prefix. Path('./Lib').glob('**/.py') emits the same paths as Path('.').glob('Lib/**/.py'). But ** is supported only in glob(), not in match(). The support of ** in match() is not documented. Would be worth to document explicitly that it is not supported.
Seems a bit strange to not have glob() and match() working the same though. Is there any reason for that?
Reacted by cccs-dm
virtuald commented on Dec 8, 2017
virtualdmannequin
Mannequin
More actions
I just ran into this also. It seems like a very strange omission that match and glob don't support the same patterns (and I'm surprised that they don't share more code).
virtuald commented on Dec 8, 2017
virtualdmannequin
Mannequin
More actions
Because of backwards compatibility (despite a statement saying it's not guaranteed for pathlib), I think the best approach would be to create a 'globmatch' function for PurePath instead of modifying the match function, and document that the match function does a different kind of matching.
This isn't a patch for cpython per se (ironically, don't have time for that this month...), but here's a MIT-licensed gist that patches pathlib2 and adds a globmatch function to it, plus associated tests extracted from pathlib2 and my own ** related tests. Works for me, feel free to do with it as you wish.
Re-opening. I'm beginning to think that the implied **/ prefix limits the usefulness of this method, as it makes it impossible to match relative paths from the left hand side, and makes it subtly incompatible with glob().
I'm going to put up a PR that reverts match() back to 3.12 behaviour, and instead adds a globmatch() method.
Activity
JonWalsh commentedon Jan 12, 2017
andunai commentedon Jan 12, 2017
Isn't this intended? According to https://docs.python.org/2/library/glob.html and wiki, typical UNIX glob pattern does not have the reqursive matching operator (
**).tiran commentedon Jan 12, 2017
The ticket is not about glob but about pathlib.
Pathlib supports ** directory globbing, but it's only documented as prefix globbing, https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob
serhiy-storchaka commentedon Jan 12, 2017
** is supported not just as a prefix. Path('./Lib').glob('**/.py') emits the same paths as Path('.').glob('Lib/**/.py'). But ** is supported only in glob(), not in match(). The support of ** in match() is not documented. Would be worth to document explicitly that it is not supported.
JonWalsh commentedon Jan 12, 2017
Seems a bit strange to not have glob() and match() working the same though. Is there any reason for that?
virtuald commentedon Dec 8, 2017
I just ran into this also. It seems like a very strange omission that match and glob don't support the same patterns (and I'm surprised that they don't share more code).
virtuald commentedon Dec 8, 2017
Because of backwards compatibility (despite a statement saying it's not guaranteed for pathlib), I think the best approach would be to create a 'globmatch' function for PurePath instead of modifying the match function, and document that the match function does a different kind of matching.
This isn't a patch for cpython per se (ironically, don't have time for that this month...), but here's a MIT-licensed gist that patches pathlib2 and adds a globmatch function to it, plus associated tests extracted from pathlib2 and my own ** related tests. Works for me, feel free to do with it as you wish.
https://gist.github.com/virtuald/dd0373bf3f26ec0730adf1da0fb929bb
24 remaining items
Merge branch 'main' into pythongh-73435-pathlib-match-recursive
Merge branch 'main' into pythongh-73435-pathlib-match-recursive
Merge branch 'main' into pythongh-73435-pathlib-match-recursive
GH-73435: Implement recursive wildcards in `pathlib.PurePath.match()` (…
barneygale commentedon May 30, 2023
Thanks for your patience all. This is now implemented in 49f90ba / #101398, and will become available in Python 3.13!
barneygale commentedon Jan 20, 2024
Re-opening. I'm beginning to think that the implied
**/prefix limits the usefulness of this method, as it makes it impossible to match relative paths from the left hand side, and makes it subtly incompatible withglob().I'm going to put up a PR that reverts
match()back to 3.12 behaviour, and instead adds aglobmatch()method.pythonGH-73435: Add `pathlib.PurePath.globmatch()`
GH-73435: Add `pathlib.PurePath.full_match()` (#114350)
barneygale commentedon Jan 26, 2024
Re-resolving. This is now implemented as
PurePath.full_match()pythonGH-73435: Add `pathlib.PurePath.full_match()` (python#114350)
pythonGH-73435: Add `pathlib.PurePath.full_match()` (python#114350)