Chapters
Python114 chapters

Exercises

Paths with pathlib

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Build the path and print its parts, name and suffix.

Python
from pathlib import Path
# build data/reports/march.csv and print parts, name, suffix
Exercise 2

Write and read a small file in one call each.

Python
from pathlib import Path
# write one line to notes.txt and print it back
Exercise 3

Find every .txt file below the folder, including inside subfolders.

Python
from pathlib import Path

Path("tree/inner").mkdir(parents=True, exist_ok=True)
for name in ["tree/a.txt", "tree/b.csv", "tree/inner/c.txt"]:
    Path(name).write_text("x", encoding="utf-8")

# print ['a.txt', 'c.txt']