OS & Path Operations: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Build paths with Path and the / operator.
Read the pieces of a path back out.
Walk up a path with .parents and look at its root.
Find where the program is running and where the user's home folder is.
Turn a messy relative path into one absolute, normalised path.
Ask whether a path exists and what kind of thing it is.
Create folders, including nested ones, without crashing on a rerun.
Create an empty file, and update a timestamp.
Read and write a whole file in one call.
Read and write binary files.
Use a Path anywhere a filename is expected.
List what is inside a folder.
Select files by pattern.
Search a whole tree.
Find out how big a file is.
Read a file's timestamps and turn them into real dates.
Rename and move a file within the same filesystem.
Delete files and empty folders.
Derive one path from another.
Express one path relative to another.
Test a path against a glob pattern without touching the disk.
Translate between os.path and pathlib.
Ask the os module about the platform you are on.
Compare os.listdir() with Path.iterdir().
Use the os functions that create and remove things.
Walk a directory tree with os.walk().
Compare os.walk() with Path.rglob() and pick the right one.
Use os.scandir() and see why it is faster.
Select files from a tree by type, size and age.
Copy files with shutil.
Copy a whole directory tree, with exclusions.
Move files and folders, including across filesystems.
Delete a directory and everything in it — carefully.
Create temporary files and folders that clean themselves up.
Measure free space and the size of a directory tree.
Find executables the way the shell does.
Create and extract archives with shutil.
Read and expand environment variables.
Inspect and change file permissions, and note what Windows does.
Manipulate paths for another operating system.
Handle names like archive.tar.gz and report.2024.final.csv.
Replace a file's contents without ever leaving it half-written.
Compare files and directories with filecmp.
Fingerprint a file's contents with hashlib.
Create and follow links, and notice what breaks.
Skip whole subtrees while walking.
Join a user-supplied path safely.
Print a directory tree the way the tree command does.
Convert files between encodings and line endings.
Bring the band together: a small reusable toolkit.
Sort a messy download folder into subfolders by file type.
Rename a batch of files by rule, safely.
Find duplicate files without hashing everything.
Report where the space has gone.
Delete files older than a cutoff — with a dry run first.
Take a timestamped backup and keep only the newest few.
Rotate a log file when it gets too big.
Generate a project skeleton from a specification.
Replace text across a whole tree, with a preview and a backup.
Mirror one directory onto another, copying only what changed.
Normalise encodings and line endings across a folder.
Export a file inventory to CSV.
Flatten a nested tree into one folder without losing files to name clashes.
Split a large folder into batches of a fixed size.
Archive old files into a dated zip and remove the originals.
Find a config file the way real tools do.
Change directory safely with a context manager.
Detect changes in a folder by polling.
Audit a tree for problems before you ship it.
Write a manifest and use it to verify or restore a tree.
This "remove empty folders" function only removes the deepest one. Explain and fix it.
import os
import tempfile
from pathlib import Path
SANDBOX = Path(tempfile.mkdtemp())
(SANDBOX / "a" / "b" / "c").mkdir(parents=True)
for dirpath, dirnames, filenames in os.walk(SANDBOX):
if not dirnames and not filenames:
os.rmdir(dirpath)
print(sorted(str(p.relative_to(SANDBOX)) for p in SANDBOX.rglob("*")))Find out what glob does and does not match.
Show what happens when the right-hand side of a join is absolute.
Understand exactly what .resolve() does and when it lies.
Show why .rename() is not portable and .replace() is.
Delete files while listing a directory, and see what goes wrong.
See how the same code behaves differently on case-insensitive filesystems.
See what Path does and does not clean up for you.
Find the cases where an existence check gives a misleading answer.
Break a naive file server, then fix it.
Discover which filenames a platform refuses.
Handle the deletions that fail halfway through.
See how filenames are encoded, and where that leaks.
Read stat() correctly, including the fields that mean different things.
Show why check-then-act is unreliable, and what to do instead.
Build a Media Organiser that files photos into folders by date.
Build a Disk Usage Analyser with a tree chart, like du.
Build an Incremental Backup that only stores what changed.
Build a Duplicate Cleaner that chooses which copy to keep.
Build a Workspace Cleaner that reclaims build artefacts and caches.
Build a Static Site Builder that copies, transforms and fingerprints files.
Build a Directory Diff tool that reports how two trees differ.
Build a Log Archiver that rotates, compresses and expires by policy.
Build a Bulk Rename Studio with pattern rules and an undo file.
Build a Project Health Report combining size, age, structure and risk.
Map pathlib against os and os.path completely, and say when each still wins.
State the rules for writing file code that runs on every platform.
Set out the safety rules for code that deletes, moves or overwrites files.
Measure the cost of every way to walk a tree, and explain the differences.
Build a File System Toolkit — the complete demonstration of this topic. Scan a messy project, report on it, organise it, deduplicate it, back it up and verify the result.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session