← Reference notes

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

Caveats

Pointers

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

Permanent ID: iil:note:w9zxazta

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.