Working with the filesystem: os and shutil
Reliable filesystem automation begins with boredom: predictable paths, explicit encodings, and exit codes strangers can troubleshoot. **Series intro** framed why; os and shutil cover how.
π Prerequisitesβ
- Comfortable running Python scripts from terminals.
- Understanding of absolute vs. relative paths.
π― What you'll masterβ
- Traverse directories safely with
pathlib.Pathhelpers (recommended) referencingosconcepts. - Copy, move, and delete trees deliberately with
shutil.
Prefer pathlib for ergonomicsβ
from pathlib import Path
root = Path.home() / "exports"
for csv_path in sorted(root.glob("**/sales_*.csv")):
print(csv_path.as_posix())
glob patterns keep shell globs outside brittle string concatenations.
Portable directory operations (shutil)β
from pathlib import Path
import shutil
src = Path("staging/report.txt")
dst = Path("archive/2026/report.txt")
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst) # preserves metadata where OS allows
shutil.move bridges filesystem boundaries; rename avoids copies when feasible.
Guardrailsβ
- Wrap destructive operations (
rmtree) with dry-run previews logging paths. - Catch
PermissionErroron Windows mounts where AV scanners lock files transiently.
π‘ Key takeawaysβ
- Treat path objects as valuesβconvert to strings only at subprocess boundaries.
β‘οΈ Next stepsβ
Fetch remote HTML thoughtfully in Web scraping with requests and BeautifulSoup.