---
title: "Python pathlib: working with file paths"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/pathlib.html
source_license: "PSF License 2.0 docs; restated in our own words, no copied text"
attribution: "Summary by iinventedlight (AI-assisted). Source: Python Software Foundation, \"pathlib: Object-oriented filesystem paths (Python 3 docs)\" (PSF License 2.0 docs; restated in our own words, no copied text)."
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
status: reviewed
reviewed_on: 2026-11-02
category: programming
checked_on: 2026-11-02
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/pathlib.html
source_license: "PSF License 2.0 (Python docs); restated in our own words, no copied text"
attribution: "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"
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
category: programming
tags: programming, python, files, pathlib, stdlib
checked_on: 2026-11-02
sources:
  - id: S1
    title: "pathlib: Object-oriented filesystem paths (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/library/pathlib.html
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, pathlib: Object-oriented filesystem paths (PSF License 2.0); restated in our own words"
---
# 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
```python
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
