Python pathlib: paths and files (Path, UTF-8, glob)
pathlib tutorial: Path, portable paths, UTF-8 read/write, existence, glob. 20 exercises. Works with plain Python files — no robot required.
Getting started
Home Assistant
Buying
pathlib tutorial: Path, portable paths, UTF-8 read/write, existence, glob. 20 exercises. Works with plain Python files — no robot required.


AI-generated illustration
Some book links below are affiliate links. Check today’s price. This lesson works with plain Python files on your computer — no robot required.
Treating paths as plain strings ("folder" + "\\" + "file.txt") quickly leads to bugs: Windows separators (\) vs POSIX (/), double slashes, ambiguous relative paths depending on the current directory, and forgotten encoding on read. The pathlib module (standard library, stable since Python 3.4+) provides Path objects that represent a path abstractly, then render it correctly for the system when needed.
A Path can be composed with the / operator (overloaded), list a directory, test file vs folder, and read or write text in one line. It has become the recommended approach for new code; the older os.path module remains readable in legacy code, but pathlib unifies the API and improves readability.
| Task | pathlib |
os.path (older OS path helpers) |
|---|---|---|
| Join parts | Path("a") / "b" / "c.txt" |
os.path.join("a", "b", "c.txt") |
| File exists? | Path("f.txt").exists() |
os.path.exists("f.txt") |
| Parent folder | Path("a/b.txt").parent |
os.path.dirname("a/b.txt") |
Prefer Path / "a" / "b" over string concatenation for user paths — it is clearer and less error-prone across Windows and POSIX.
Path("relative") is relative to the current working directory; Path("/absolute") (on Unix) or Path("C:/Users/...") (on Windows) fix a root. The / operator avoids manual concatenation: each segment is a path piece, and the final representation respects the OS.
from pathlib import Path
root = Path(".")
file = root / "data" / "notes.txt"
Best practices: in new code, prefer pathlib.Path and / over repeated os.path.join. For paths without disk access (pure logic), PurePath exists, but Path is enough most of the time.
The methods read_text and write_text wrap file opening and full read/write in memory — handy for small or medium files (config, export). For very large files, prefer streaming (open() or Path.open()).
p = Path("hello.txt")
p.write_text("Hello\n", encoding="utf-8")
print(p.read_text(encoding="utf-8"))
Encoding: on Windows, the historical default encoding is not always UTF-8; explicitly set encoding="utf-8" for text to avoid UnicodeDecodeError when deploying on another OS.
Before reading, we often check exists(); is_file() and is_dir() distinguish file and folder. resolve() can return an absolute, normalised path (resolving ..), useful to compare two paths or display them clearly.
if p.exists():
print("found")
if p.is_file():
...
glob)iterdir() enumerates the direct children of a directory. glob("**/*.py") or rglob("*.py") apply recursive patterns — handy to process all files of a type without writing a manual os.walk loop.
for child in Path(".").iterdir():
print(child.name)
Best practices: remember that glob depends on the real disk contents; in tests, isolate a temporary directory (tmp_path with pytest) to stay reproducible.
Path, PurePath types, methods.encoding and text mode.The exercises lock in the reflexes: composition with /, UTF-8, existence tests. Solutions are hidden by default.
Exercise 1 — Create Path("a.txt") and print its representation with print. Easy
from pathlib import Path
print(Path("a.txt"))
Exercise 2 — Build the path folder/sub/file.txt with the / operator from Path("folder"). Easy
from pathlib import Path
p = Path("folder") / "sub" / "file.txt"
print(p)
Exercise 3 — Write "hi" into out.txt with write_text and encoding="utf-8". Easy
from pathlib import Path
Path("out.txt").write_text("hi", encoding="utf-8")
Exercise 4 — Read the contents of out.txt with read_text. Easy
from pathlib import Path
print(Path("out.txt").read_text(encoding="utf-8"))
Exercise 5 — Check whether data.txt exists with .exists(). Easy
from pathlib import Path
print(Path("data.txt").exists())
Exercise 6 — Print the file name alone (name) for /tmp/a/b.txt (use Path). Easy
from pathlib import Path
print(Path("/tmp/a/b.txt").name)
Exercise 7 — Print the parent folder with .parent for Path("folder/file.py"). Easy
from pathlib import Path
print(Path("folder/file.py").parent)
Exercise 8 — List the files in the current directory whose suffix is .txt (iterdir loop + .suffix test). Intermediate
from pathlib import Path
for p in Path(".").iterdir():
if p.is_file() and p.suffix == ".txt":
print(p)
Exercise 9 — Use glob("*.py") in the current folder to list .py files. Intermediate
from pathlib import Path
for p in Path(".").glob("*.py"):
print(p)
Exercise 10 — Create a tmp folder with Path("tmp").mkdir(exist_ok=True). Intermediate
from pathlib import Path
Path("tmp").mkdir(exist_ok=True)
Exercise 11 — Resolve an absolute path with .resolve() from Path("README.md"). Intermediate
from pathlib import Path
print(Path("README.md").resolve())
Exercise 12 — Best practice: explain why Path / "a" / "b" is better than string concatenation for user paths. Intermediate
# Path normalises separators for the OS and avoids double slashes or missing ones.
Exercise 13 — Open in binary with Path: Path("b.bin").read_bytes() (or write_bytes). Print the length of the bytes read. Intermediate
from pathlib import Path
p = Path("b.bin")
p.write_bytes(b"\x00\x01")
print(len(p.read_bytes()))
Exercise 14 — Walk rglob("*.md") from Path(".") to find Markdown files recursively (watch performance on large trees). Intermediate
from pathlib import Path
for p in Path(".").rglob("*.md"):
print(p)
Exercise 15 — Function read_if_exists(path: str) -> str | None: return the text or None if the file does not exist (FileNotFoundError handled cleanly). Hard
from pathlib import Path
def read_if_exists(path: str): p = Path(path) if not p.exists(): return None return p.read_text(encoding=“utf-8”)
Exercise 16 — Rename old.txt to new.txt with .rename. Hard
from pathlib import Path
Path("old.txt").rename(Path("new.txt"))
Exercise 17 — Count the lines of a text file with read_text and splitlines() (handle empty files safely). Hard
from pathlib import Path
def line_count(p):
return len(Path(p).read_text(encoding="utf-8").splitlines())
Exercise 18 — Path.home() / "Documents": what is home() for? Hard
# The current user's home directory (varies by OS).
Exercise 19 — Security best practice: why avoid eval(Path(...).read_text())? Hard
# A text file may contain arbitrary code that would then execute: uncontrolled code execution.
Exercise 20 — Write a function copy_text(src, dst) with read_text / write_text and encoding="utf-8" everywhere. Hard
from pathlib import Path
def copy_text(src, dst): t = Path(src).read_text(encoding=“utf-8”) Path(dst).write_text(t, encoding=“utf-8”)
Create a sensor_logs/ folder, write three .txt files with Path.write_text(..., encoding="utf-8"), then list them with Path("sensor_logs").glob("*.txt") and print the line count of each.
Next: Classes and OOP — structure data and behaviour with classes. When one folder of scripts becomes a project, the pyproject.toml packaging guide shows how to declare it. Also in this series: types and input · higher or lower terminal game. Full outline: intermediate Python course.
Practice books (affiliate links):
Author: Emmanuel Sauvage · updated 31 October 2026.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).