Lesson 34 / 49
Working with Paths (pathlib)
Handle filesystem paths portably with pathlib.Path instead of manual string concatenation.
By Rahul Agarwal · 15 years in web development · Updated 1 Oct 2026
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)