pathlib

Python's standard-library module for working with file and folder paths as objects.

pathlib represents a filesystem path as a Path object instead of a plain string. You build paths with the / operator (Path("data") / "file.txt") and read their parts with .name, .stem, and .suffix.

Because it behaves the same on Windows, macOS, and Linux, pathlib is the modern, cross-platform way to find, inspect, and rename files.

Example
from pathlib import Path

p = Path("photos") / "vacation.JPG"
print(p.name)
print(p.suffix.lower())
Output
vacation.JPG
.jpg

Where this shows up in real Python

pathlib is the backbone of file automation: finding, reading, writing, and renaming files without fragile string paths.

Commonly used pathlib tools

The Path object carries useful methods and properties:

  • Path('data') / 'in.csv' — join paths with /
  • .exists() — does it exist
  • .glob('*.txt') — find matching files
  • .read_text() / .write_text() — read or write a whole file
  • .suffix / .name / .parent — extension, filename, folder
  • .mkdir(parents=True, exist_ok=True) — create folders safely

Official documentation: Python Library Reference: pathlib

Related terms