Globbing and Iterating
Find files with glob patterns.
Finding Files
pathlib makes it easy to list directory contents and search for files matching a pattern. The key methods are iterdir, glob, and rglob.
Setting Up a Sample Tree
To explore these methods we first build a small directory of files. This setup uses only the standard library.
from pathlib import Path
root = Path('sample')
root.mkdir(exist_ok=True)
(root / 'a.txt').write_text('1')
(root / 'b.txt').write_text('2')
(root / 'c.log').write_text('3')
print('created files')All lessons in this course
- The Path Object
- Reading and Writing Files
- Globbing and Iterating
- Path Manipulation