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
pathlibandoscan get CSV files recursively, butpathlibis 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')andos.walk('.')give back a generator.
