0Pricing
Python Academy · Lesson

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

  1. The Path Object
  2. Reading and Writing Files
  3. Globbing and Iterating
  4. Path Manipulation
← Back to Python Academy