Python pathlib: working with file paths
Python pathlib: working with file paths
Summary
pathlib (Python 3.4+) represents filesystem paths as objects. Path joins parts with /, exposes .name, .stem, .suffix and .parent, and has methods for checking, reading, writing, listing and globbing. Prefer it to string concatenation and most os.path calls.
Draft. License: CC BY 4.0 (this summary). Sources: [S1] Python Software Foundation, "pathlib: Object-oriented filesystem paths (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text).
Example
from pathlib import Path
out = Path("build") / "reports"
out.mkdir(parents=True, exist_ok=True)
for f in Path("data").glob("*.csv"):
(out / f.with_suffix(".txt").name).write_text(f.read_text().upper())
Facts
- id: F1 claim: pathlib was added in Python 3.4; Path is the concrete class for the current OS, while PurePath classes do path math without touching the filesystem. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F2 claim: The / operator joins paths: Path('data') / 'raw' / 'a.csv'. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F3 claim: p.name is the final component, p.stem drops the last suffix, p.suffix is the extension including the dot, p.parent is the containing directory. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F4 claim: Path.cwd() and Path.home() return the current and home directories. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F5 claim: exists(), is_file() and is_dir() test the filesystem. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F6 claim: read_text()/write_text() and read_bytes()/write_bytes() open, read or write and close the file in one call. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F7 claim: mkdir(parents=True, exist_ok=True) creates missing parent directories and does not error if the directory exists. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F8 claim: iterdir() lists a directory; glob('*.txt') matches a pattern; rglob() matches recursively. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F9 claim: with_suffix('.json') returns a new path with the extension replaced; resolve() makes a path absolute and resolves symlinks. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
Caveats
- Path objects are immutable; methods return new paths.
- Most standard-library functions that take a filename accept Path objects (os.PathLike).
Pointers
- id: P1 answers: Full details and updates where: https://docs.python.org/3/library/pathlib.html access: free, public web page
Source: https://docs.python.org/3/library/pathlib.html · License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted), condensed and paraphrased; sources: Python Software Foundation, Python 3 documentation, pathlib: Object-oriented filesystem paths (PSF License 2.0); restated in our own words
Cite this
iinventedlight Library. 2026. "Python pathlib: working with file paths." iinventedlight Library, iil:note:w9zxazta, version of 2026-11-02. https://iinventedlight.com/library/reference/python-docs/python-pathlib/. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/pathlib.html.
@misc{iil_note_w9zxazta,
author = {{iinventedlight Library}},
title = {{Python pathlib: working with file paths}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-02},
url = {https://iinventedlight.com/library/reference/python-docs/python-pathlib/},
note = {Permanent ID iil:note:w9zxazta. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/pathlib.html.},
}
For language models and agents: start at /llms.txt. This page in Markdown: /library/reference/python-docs/python-pathlib.md.