Skip to content
Python Basics: Working with Files Using pathlib

Python Basics: Working with Files Using pathlib

October 11, 2026

Python’s standard-library pathlib module represents filesystem paths as objects. It can join paths, read small text files, and find matching filenames without building shell commands.

Use Python 3 on Linux. Run the shell commands in order in the same shell, beginning in a fresh temporary directory:

cd "$(mktemp -d)"

Create a directory and a file

The following code creates notes/todo.txt. write_text() replaces an existing file’s contents, so keep this example in the practice directory.

Save as demo_pathlib_1.py:

from pathlib import Path

folder = Path("notes")
folder.mkdir()
path = folder / "todo.txt"
path.write_text("Read the docs\n", encoding="utf-8")

print(path)
print(path.read_text(encoding="utf-8"), end="")

Run:

python3 demo_pathlib_1.py

Output:

notes/todo.txt
Read the docs

The / operator joins path components. Constructing a Path does not create anything; mkdir() and write_text() perform the filesystem operations. mkdir() raises FileExistsError if notes already exists, which helps reveal an accidental rerun.

Inspect a path

Save as demo_pathlib_2.py:

from pathlib import Path

path = Path("notes/todo.txt")
print(path.name)
print(path.stem)
print(path.suffix)
print(path.parent)
print(path.is_file())

Run:

python3 demo_pathlib_2.py

Output:

todo.txt
todo
.txt
notes
True

The name-related properties describe the path’s text. is_file() checks the filesystem. Relative paths such as this one are interpreted from the process’s current working directory.

Find text files

This creates two more practice files; rerunning these writes would overwrite them.

Save as demo_pathlib_3.py:

from pathlib import Path

folder = Path("notes")
(folder / "ideas.txt").write_text("Try pathlib\n", encoding="utf-8")
(folder / "data.csv").write_text("name,value\n", encoding="utf-8")

for path in sorted(folder.glob("*.txt")):
    if path.is_file():
        print(path.name)

Run:

python3 demo_pathlib_3.py

Output:

ideas.txt
todo.txt

glob("*.txt") searches this directory; rglob("*.txt") also searches below it. Sorting makes the displayed order predictable because directory enumeration order is not guaranteed.

read_text() loads the whole file into memory. For large files, use path.open() in a with block and iterate over lines instead.

Reference

Python pathlib documentation

Acknowledgements

This post was created with assistance from AI.

Last updated on