SkillByAIOpen interactive version →

Lesson 34 / 49

Working with Paths (pathlib)

Handle filesystem paths portably with pathlib.Path instead of manual string concatenation.

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)