PurePath.match
match('*.py') is true for any path whose LAST component ends in .py — a relative pattern is matched from the right. full_match('**/*.py') matches the entire path, with ** spanning any number of folders, exactly like Path.glob.
Demo
from pathlib import PurePosixPath p = PurePosixPath('src/pkg/core.py') (p.match('*.py'), p.full_match('*.py'))
For 'src/pkg/core.py' the relative pattern '*.py' matches through match() (only the last part is compared) but not through full_match(), which needs the whole path. '**/*.py' works in both, but for different reasons: full_match treats ** as any number of folders, while match treats ** like * and still compares from the right. An empty pattern is a ValueError for match(), and the demo never gets to full_match(); on its own, full_match('') only matches the empty path '.'.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| pattern | str | PurePath | yes | Glob pattern: * ? [seq] [!seq] within one component; ** (full_match only) for any number of components. |
| case_sensitive | bool | None | no (None) | None follows the flavour: case-sensitive for POSIX, insensitive for Windows. |
Return value
bool — Whether the path matches the pattern.
Common patterns
from pathlib import PurePosixPath sources = [p for p in paths if PurePosixPath(p).full_match('src/**/*.py')]
from pathlib import Path images = [p for p in Path('photos').iterdir() if p.match('*.jp*g', case_sensitive=False)]
from pathlib import PurePosixPath IGNORE = ['**/__pycache__/**', '**/*.pyc', '.git/**'] def ignored(p): return any(PurePosixPath(p).full_match(pat) for pat in IGNORE)
Examples
Pitfalls
from pathlib import PurePosixPath PurePosixPath('vendor/lib/x.py').match('*.py')
from pathlib import PurePosixPath PurePosixPath('vendor/lib/x.py').full_match('*.py')
from pathlib import PurePosixPath PurePosixPath('README.MD').full_match('*.md')
from pathlib import PurePosixPath PurePosixPath('README.MD').full_match('*.md', case_sensitive=False)
When to use
- Filtering paths you already have (from a list, a zip, a database)
- Ignore / include rules with ** (full_match)
- Finding files on disk → Path.glob / rglob
- Regular-expression power → re.fullmatch on as_posix()
Notes
FAQ
match() compares a relative pattern against the end of the path and treats ** like *. full_match() (3.13+) compares against the whole path and treats ** as any number of folders, the same as glob().