Python pathlib match function doesn't work

Viewed 1041

pathlib match(pattern) is documented as matching a path against a provided glob-style pattern but it doesn't work

>>> Path("w/x/y/z").mkdir(parents=True)
>>> list(Path().glob("w/**/z"))
[PosixPath('w/x/y/z')]
>>> Path("w/x/y/z").match("w/**/z")
False

Shouldn't that return true?

1 Answers

The glob pattern of ** does not go through path delimiters. At least the path.match() function does not have it implemented. Maybe try path.rglob() which does recursive globbing.

Try:

In [1]: from pathlib import Path

In [2]: p = Path("w/z/y/z")

In [3]: p.mkdir(parents=True)

In [5]: p.match("w/*/*/z")
Out[5]: True
Related