Find all files matching a pattern


# With pathlib.rglob() - concise
from pathlib import Path
txt_files = list(Path('.').rglob('*.txt'))
 
# With os.walk() - more manual
import os
txt_files = []
for root, dirs, files in os.walk('.'):
    for file in files:
        if file.endswith('.txt'):
            txt_files.append(os.path.join(root, file))
  • Both pathlib and os can get CSV files recursively, but pathlib is more concise because it encapsulates the pattern matching and traversal in one function. However, os.walk() is better when you need to skip certain directories during traversal.
  • pathlib’s lack of mid-traversal pruning is a real limitation for large directory trees!

Info

Both Path('.').rglob('*.txt') and os.walk('.') give back a generator.