Photo by Chiara F / Unsplash

TIL - Python's pathlib.Path Makes File Operations Readable and Composable

Today I Learned Apr 21, 2026

The old os.path.join(base, "subdir", "file.txt") is verbose and error-prone. pathlib.Path uses / operator for joining and has a clean API.

from pathlib import Path

base = Path("/data/projects")
config = base / "app" / "config.yaml"

config.parent.mkdir(parents=True, exist_ok=True)
config.write_text("key: value")
print(config.suffix)   # .yaml
print(config.stem)     # config

Tags