Lesson 32 / 47

Working with Paths (pathlib)

The modern, object-oriented way to handle filesystem paths.

Path basics

pathlib.Path replaces manual string concatenation for paths and works the same across operating systems.

from pathlib import Path

p = Path("data") / "2024" / "report.csv"
print(p)                 # data/2024/report.csv
print(p.name)             # report.csv
print(p.suffix)           # .csv
print(p.exists())         # False (unless it's really there)