Pcornel
Automation · Python Automation

pathlib

Cheat sheet · https://pcornel.dk/topic.php?topic=5265

Back to the Topic

Answer

pathlib handles file paths as objects in Python, such as Path('logs') / 'run.txt', with methods like exists(), glob() and read_text(), working the same on Windows and Linux.

Remember

  • Path objects handle paths cross-platform.
  • Use / to join paths.
  • Methods: exists(), glob(), read_text().

First checks

  • python3 -c 'from pathlib import Path; print(Path(".").resolve())' — resolve paths
  • Path.glob / rglob usage — file discovery
  • Path traversal checks — safety

Verify in this order

path objects → file operations

Where it usually breaks

Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5265

Confirmation

Confirm action

Continue?