Learn pathlib

Object-oriented file system paths that work the same on every OS

13 lessons · 2 hours · 4 levels · covers pathlib Python 3.13

pathlib represents file system paths as objects with methods for joining, inspecting, reading, writing, globbing and resolving them. It replaces most uses of os.path, glob and open with one readable API that handles Windows and POSIX differences. It's the modern default for any Python code that touches files.

Before you start

pathlib builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 29 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 56 min in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 1 hour in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The pathlib tree

pathlibPath objects and the / operatorPath objects and the /operatorNames, suffixes and parentsNames, suffixes andparentsReading and writing filesReading and writingfilesChecking and listing pathsChecking and listingpathsGlobbing with glob and rglobGlobbing with globand rglobCreating, moving and deletingCreating, moving anddeletingAbsolute, resolved and relative pathsAbsolute, resolvedand relative pathsReading and writing filesReading and writingfilesErrors and try/exceptErrors and try/exceptType hints basicsType hints basicsInheritance, super() and compositionInheritance, super()and composition
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Path basics

  1. Path objects and the / operator · Essentials · 7 min

    Create Path objects, join them with /, and get the current and home directories.

  2. Names, suffixes and parents · Essentials · 8 min

    Take paths apart with name, stem, suffix, parent and parts, and derive new paths with with_name, with_stem and with_suffix.

Files and directories

  1. Reading and writing files · Essentials · 7 min

    Read and write whole files with read_text, write_text, read_bytes and write_bytes, or open a Path for streaming.

  2. Checking and listing paths · Essentials · 7 min

    Test whether paths exist and what they are, and list a directory's children with iterdir.

  3. Creating, moving and deleting · Practical · 9 min

    Make directories, touch, rename, replace and delete files and empty folders with Path methods.

Searching and matching

  1. Globbing with glob and rglob · Practical · 9 min

    Find files by pattern with glob and recursively with rglob or **.

  2. Absolute, resolved and relative paths · Practical · 9 min

    Normalise paths with resolve and absolute, expand ~, and compute paths relative to a base with relative_to.

  3. Walking trees and file metadata · Advanced · 11 min

    Traverse directory trees with Path.walk, prune subtrees, and use stat results for size and age based processing.

  4. Matching paths and safe joins · Advanced · 10 min

    Test paths against glob patterns with match and full_match, and confine user-supplied paths to a base directory.

Pure paths and portability

  1. Pure paths and Windows vs POSIX · Advanced · 10 min

    Manipulate paths for another operating system with PurePosixPath and PureWindowsPath, without touching the disk.

Design and internals

  1. Accepting paths in your own APIs · Expert · 9 min

    Design functions that accept str, bytes and PathLike arguments correctly, with accurate type hints.

  2. Subclassing Path and with_segments · Expert · 11 min

    Extend Path with domain-specific methods using the 3.12+ subclassing support and with_segments.

  3. pathlib vs os: performance trade-offs · Expert · 9 min

    Know where pathlib costs more than os and os.path, and how to keep hot file-system loops fast.