Python pathlib · Exercises
Core Python

Python pathlib Exercises

4 exercises that build on the Python pathlib lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: pathlib's Path is an object that represents a file or folder location, with operators and methods to join, take apart, check, read, list, rename and delete paths the same way on every operating system. By the end you can build paths with /, read their name, suffix and parent, create folders, read and write small files, list files with glob and rglob, and rename or delete them safely. You will practise: Build a report path; Predict the path pieces; Fix the note saver; Inspect a folder.

0 / 4 completed

Python pathlib practice exercises

1. Build a report path

Write code Easy

Write report_path(base, year, month) that returns a Path (not a string) in this shape: <base>/reports/<year>/sales_<month>.csv, where the month always has two digits. For example report_path("exports", 2025, 3) is exports/reports/2025/sales_03.csv. Join the pieces with /. The program prints the path for the starter call.

Your code
from pathlib import Path

def report_path(base, year, month):
    # build exports/reports/2025/sales_03.csv as a Path
    pass

print(report_path("exports", 2025, 3))

2. Predict the path pieces

Predict the output Easy

Read the code and type exactly what it prints. Nothing is created on disk, so think only about the pieces of the path. Then press Check.

Read this code
from pathlib import Path

p = Path("photos/2025/trip.final.jpg")
print(p.stem)
print(p.suffixes)
print(p.with_suffix(".png"))
print(p.parent.name)
print(p.parts[-2:])

3. Fix the note saver

Fix the bug Medium

save_note(folder, title, text) should create the folder (with any missing parents), save the text in <title>.txt inside it and return the Path. It crashes with a FileNotFoundError, and it must also work when the folder already exists. Fix the bug.

Your code
from pathlib import Path
import shutil

shutil.rmtree("demo_notes", ignore_errors=True)

def save_note(folder, title, text):
    path = Path(folder) / f"{title}.txt"
    path.parent.mkdir()
    path.write_text(text)
    return path

note = save_note("demo_notes/2025/work", "todo", "Call Sam")
print(note)
print(note.read_text())

4. Inspect a folder

Write code Hard

Write inspect_folder(folder) (the argument is a string) that returns a dictionary with three keys:

  • "files": a sorted list with every file inside the folder and its sub-folders, as strings relative to the folder and written with /, such as "docs/guide.txt";
  • "bytes": the combined size of those files in bytes;
  • "suffixes": a dictionary that maps each suffix (".txt", or "" when a file has none) to the number of files with it.

Folders themselves are not counted. The starter creates a small demo folder and prints the result.

Your code
from pathlib import Path

def inspect_folder(folder):
    root = Path(folder)
    # collect the files, their total size and a count per suffix
    return {"files": [], "bytes": 0, "suffixes": {}}

demo = Path("inspect_demo")
(demo / "docs").mkdir(parents=True, exist_ok=True)
(demo / "a.txt").write_text("hello")
(demo / "docs" / "guide.txt").write_text("0123456789")
(demo / "README").write_text("hi")
print(inspect_folder("inspect_demo"))

Next lessonPython Classes and Objects

More Python pathlib examples · All Python exercises