Skip to main content

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.Path helpers (recommended) referencing os concepts.
  • 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 PermissionError on 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.