Python pathlib Guide — Modern File Path Handling

Learn Python pathlib for file system operations: creating paths, reading and writing files, directory traversal, glob patterns, and replacing os.path.

Why pathlib Over os.path?

The old way uses string concatenation: os.path.join(base_dir, "data", "output.csv"). With pathlib.Path, paths are objects with methods: base_dir / "data" / "output.csv". This is more readable, cross-platform, and avoids common bugs.

Creating and Navigating Paths

from pathlib import Path

# Create paths
p = Path("/home/user/documents/report.pdf")
p = Path.home() / "documents" / "report.pdf"
p = Path.cwd() / "src" / "main.py"

# Navigation
p.parent         # /home/user/documents
p.name           # report.pdf
p.stem           # report
p.suffix         # .pdf
p.parts          # ('/', 'home', 'user', 'documents', 'report.pdf')

# Check existence
p.exists()       # True/False
p.is_file()      # True if it's a regular file
p.is_dir()       # True if it's a directory

Reading and Writing Files

p = Path("data/config.json")

# Read
content = p.read_text(encoding="utf-8")
data = p.read_bytes()

# Write
p.write_text("Hello, World!", encoding="utf-8")
p.write_bytes(b"")

# Create directories
Path("output/reports/2026").mkdir(parents=True, exist_ok=True)

# Copy, rename, unlink
import shutil
shutil.copy(src_path, dest_path)
p.rename(p.with_suffix(".bak"))
p.unlink()  # delete file

Glob Patterns and Directory Traversal

src = Path("src")

# Find all Python files recursively
for py_file in src.rglob("*.py"):
    print(py_file)

# List immediate contents
for item in Path(".").iterdir():
    print(item.name, "dir" if item.is_dir() else "file")

# Non-recursive glob
for html in Path("blog").glob("*-guide.html"):
    print(html.stem)

Frequently Asked Questions

Is pathlib available in all Python versions?

pathlib is in the standard library since Python 3.4. All currently supported Python versions (3.9+) include it. No installation required.

Can pathlib handle URLs?

No. pathlib.Path is for filesystem paths only. For URLs, use urllib.parse or the third-party yarl library.