pathlib

noun

say it: “PATH-lib” /ˈpæθlɪb/

Python's built-in module for working with file and folder paths, which joins the parts with a slash and reads a file's name and extension.

What is pathlib?

pathlib is Python's standard module for file and folder paths. A Path joins parts with / and reads a file's name, stem, suffix and parent folder, on Windows and on a Mac alike.

from pathlib import Path
photo = Path.home() / "Downloads" / "beach.jpg"
print(photo.name, photo.stem, photo.suffix)   # beach.jpg beach .jpg
print(photo.parent.name)                      # Downloads

In a sentence

  • “The sorter uses pathlib to find every .pdf in Downloads.”
  • “With pathlib, the same code builds paths on Windows and on a Mac.”

Level: Journeyman · More Python terms at this level

Updated