Stop 06 · Files, Errors & ModulesTopics · 4/9

File Paths with pathlib

Part of the Python Roadmap

In short

Gluing folder names together with + and slashes breaks the moment your code runs on a different operating system. pathlib treats paths as smart objects that just work everywhere.

pathlib.Path treats file paths as objects, so you can join them with /, check if they exist, list folders and read or write files in a way that works on Windows, macOS and Linux.

The jaadu follows a trail of signposts through a maze of nested cardboard folders, holding a little map.

Think of it like this

A postal address that formats itself

Writing an address by hand, you might forget a comma or put the PIN code in the wrong place. pathlib is like an address label printer: you give it state, city and street, and it prints the correct format for wherever the parcel is going, whether that's Windows or Linux.

Watch it happen

Path('project') / 'data' / 'sales.csv'

project/
data/
sales.csv
notes.txt
src/
main.py

1/4Path('project') points at the project folder.

The big ideas

What you need to know

Join with /

Path('data') / 'sales.csv' builds the right path on any OS. No more worrying about \ vs /.

Ask questions

.exists(), .is_file() and .is_dir() tell you what's on disk before you use it.

Pieces of a path

.name is 'sales.csv', .stem is 'sales', .suffix is '.csv' and .parent is the folder above.

Find files

.glob('*.csv') finds matching files in one folder and .rglob('*.csv') searches every subfolder too.

See it in code

Walk through the code

python
1from pathlib import Path2 3base = Path(__file__).parent        # folder of this script4data_dir = base / "data"5data_dir.mkdir(exist_ok=True)6 7report = data_dir / "report.txt"8report.write_text("Total sales: Rs 48,500\n", encoding="utf-8")9print(report.read_text(encoding="utf-8"))10 11print(report.name)       # report.txt12print(report.stem)       # report13print(report.suffix)     # .txt14print(report.exists())   # True15 16for csv_file in data_dir.glob("*.csv"):17    print("Found:", csv_file.name)18 19backup = report.with_suffix(".bak")20report.rename(backup)

1/6Path(__file__).parent is the script's own folder, so the code works no matter where you run it from.

Build paths relative to the script, create a folder, write and read a file, inspect it and rename it.

Save yourself the pain

Common mistakes

Oops

Building paths with 'data' + '\\' + name, which breaks on macOS and Linux.

Do this instead

Use Path('data') / name and let pathlib handle separators.

Oops

Using paths relative to wherever you ran the command, so the script fails from another folder.

Do this instead

Anchor them with Path(__file__).parent.

Your mission

Downloads tidier

~25 min

Quick check

For p = Path('reports/2026/june.csv'), what is p.stem?

Go deeper

  1. 1Build a path with Path('data') / 'sales.csv' instead of joining strings.
  2. 2List every .csv file in a folder with Path.glob('*.csv').
  3. 3Use Path(__file__).parent to find files next to your script.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge