Path.glob(pattern) searches relative to a Path and yields matching path objects rather than strings. Convert the iterator to a list when you need to inspect all matches. section 12.4.1
Find matching files
from pathlib import Path cur_path = Path("sample_data") list(cur_path.glob("*json")) # [PosixPath('sample_data/anscombe.json')] list(cur_path.glob("?.tmp")) # [PosixPath('sample_data/1.tmp'), PosixPath('sample_data/2.tmp')]
Use matches in a filesystem task
from pathlib import Path size = 0 for file in Path().rglob("*.test"): if not file.is_symlink(): size += file.stat().st_size print(size)
list(cur_path.glob("*"))
[PosixPath('anscombe.json'),
PosixPath('.ipynb_checkpoints'),
PosixPath('README.md.old'),
PosixPath('1.tmp'),
PosixPath('2.tmp'),
PosixPath('california_housing_train.csv'),
PosixPath('california_housing_test.csv'),
PosixPath('mnist_test.csv'),
PosixPath('mnist_train_small.csv')]
list(cur_path.glob("*json"))
[PosixPath('anscombe.json')]
list(cur_path.glob("?.tmp"))
[PosixPath('1.tmp'), PosixPath('2.tmp')]
list(cur_path.glob("[0-9].tmp"))
[PosixPath('1.tmp'), PosixPath('2.tmp')]
Compare wildcard, suffix, single-character, and character-class patterns with their resulting Path objects. section 12.4.1
