pathlib.Path.match and **

pathlib.Path.match() is documented as:

Match this path against the provided glob-style pattern

Under Path.glob it says:

The “ ** ” pattern means “this directory and all subdirectories, recursively”.

Is that supposed to work with match() as well? It seems that would be very useful, but either it’s broken or I don’t understand how it works.

>>> pathlib.Path("x/a.b").match("x/**/*.*")
>>> pathlib.Path("x/y/a.b").match("x/**/*.*")
>>> pathlib.Path("x/y/z/a.b").match("x/**/*.*")

Should these not all be True?

The first one cannot be True, because the match pattern presumes a middle level in the path, and the path to be matched does not contain one. In that example, ** matches a.b, leaving nothing for *.* to match.

The third one seems like it should be True.

I assumed they would be True because, if these files exist, Path.glob() will actually match them:

>>> list(pathlib.Path(".").glob("x/**/*.*"))
[WindowsPath('x/a.b'), WindowsPath('x/y/a.b'), WindowsPath('x/y/z/a.b')]

Apparently it’s a bug Issue 29249: Pathlib glob ** bug - Python tracker

1 Like