Python · Intermediate Python

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.

Q1Path BasicsEasyMust Do

Build paths with Path and the / operator.

Q2Path PartsEasyMust Do

Read the pieces of a path back out.

Q3More Path PartsEasy

Walk up a path with .parents and look at its root.

Q4cwd & homeEasy

Find where the program is running and where the user's home folder is.

Q5resolve()Easy

Turn a messy relative path into one absolute, normalised path.

Q6Existence ChecksEasyMust Do

Ask whether a path exists and what kind of thing it is.

Q7mkdir()EasyMust Do

Create folders, including nested ones, without crashing on a rerun.

Q8touch()Easy

Create an empty file, and update a timestamp.

Q9read_text & write_textEasyMust Do

Read and write a whole file in one call.

Q10BytesEasy

Read and write binary files.

Q11Path.open()Easy

Use a Path anywhere a filename is expected.

Q12iterdir()EasyMust Do

List what is inside a folder.

Q13glob()EasyMust Do

Select files by pattern.

Q14rglob()Easy

Search a whole tree.

Q15stat() SizeEasy

Find out how big a file is.

Q16stat() TimesEasyMust Do

Read a file's timestamps and turn them into real dates.

Q17rename()Easy

Rename and move a file within the same filesystem.

Q18DeletingEasy

Delete files and empty folders.

Q19with_suffix()Easy

Derive one path from another.

Q20relative_to()Easy

Express one path relative to another.

Q21match()Easy

Test a path against a glob pattern without touching the disk.

Q22os.pathEasy

Translate between os.path and pathlib.

Q23os ConstantsEasy

Ask the os module about the platform you are on.

Q24os.listdir()Easy

Compare os.listdir() with Path.iterdir().

Q25os File FunctionsEasy

Use the os functions that create and remove things.

Q26os.walk()MediumMust Do

Walk a directory tree with os.walk().

Q27walk vs rglobMedium

Compare os.walk() with Path.rglob() and pick the right one.

Q28os.scandir()Medium

Use os.scandir() and see why it is faster.

Q29Filtering a TreeMedium

Select files from a tree by type, size and age.

Q30shutil.copyMediumMust Do

Copy files with shutil.

Q31shutil.copytreeMedium

Copy a whole directory tree, with exclusions.

Q32shutil.moveMedium

Move files and folders, including across filesystems.

Q33shutil.rmtreeMediumMust Do

Delete a directory and everything in it — carefully.

Q34tempfileMediumMust Do

Create temporary files and folders that clean themselves up.

Q35Disk UsageMedium

Measure free space and the size of a directory tree.

Q36PATH & whichMedium

Find executables the way the shell does.

Q37ArchivesMedium

Create and extract archives with shutil.

Q38EnvironmentMedium

Read and expand environment variables.

Q39PermissionsMedium

Inspect and change file permissions, and note what Windows does.

Q40Pure PathsMedium

Manipulate paths for another operating system.

Q41Double ExtensionsMedium

Handle names like archive.tar.gz and report.2024.final.csv.

Q42Atomic WritesMediumMust Do

Replace a file's contents without ever leaving it half-written.

Q43Comparing FilesMedium

Compare files and directories with filecmp.

Q44Hashing FilesMedium

Fingerprint a file's contents with hashlib.

Q45SymlinksMedium

Create and follow links, and notice what breaks.

Q46Walking with PruningMedium

Skip whole subtrees while walking.

Q47Safe JoiningMediumMust Do

Join a user-supplied path safely.

Q48Tree PrintingMedium

Print a directory tree the way the tree command does.

Q49Line Endings & EncodingMedium

Convert files between encodings and line endings.

Q50Path UtilitiesMedium

Bring the band together: a small reusable toolkit.

Q51OrganisingMediumMust Do

Sort a messy download folder into subfolders by file type.

Q52Bulk RenamingMedium

Rename a batch of files by rule, safely.

Q53DuplicatesMediumMust Do

Find duplicate files without hashing everything.

Q54Size ReportsMedium

Report where the space has gone.

Q55Cleanup by AgeMediumMust Do

Delete files older than a cutoff — with a dry run first.

Q56BackupsMedium

Take a timestamped backup and keep only the newest few.

Q57Log RotationMedium

Rotate a log file when it gets too big.

Q58ScaffoldingMediumMust Do

Generate a project skeleton from a specification.

Q59Find & ReplaceMedium

Replace text across a whole tree, with a preview and a backup.

Q60SyncMediumMust Do

Mirror one directory onto another, copying only what changed.

Q61Batch ConversionMedium

Normalise encodings and line endings across a folder.

Q62Metadata ExportMedium

Export a file inventory to CSV.

Q63FlatteningMedium

Flatten a nested tree into one folder without losing files to name clashes.

Q64ChunkingMedium

Split a large folder into batches of a fixed size.

Q65ArchivingMedium

Archive old files into a dated zip and remove the originals.

Q66Config DiscoveryMedium

Find a config file the way real tools do.

Q67Working DirectoryMedium

Change directory safely with a context manager.

Q68WatchingMedium

Detect changes in a folder by polling.

Q69AuditingMedium

Audit a tree for problems before you ship it.

Q70ManifestsMedium

Write a manifest and use it to verify or restore a tree.

Q71DebuggingHard

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("*")))
Q72glob SurprisesHard

Find out what glob does and does not match.

Q73Joining TrapsHardMust Do

Show what happens when the right-hand side of a join is absolute.

Q74resolve() DetailsHard

Understand exactly what .resolve() does and when it lies.

Q75rename vs replaceHardMust Do

Show why .rename() is not portable and .replace() is.

Q76Mutating While IteratingHard

Delete files while listing a directory, and see what goes wrong.

Q77Case SensitivityHard

See how the same code behaves differently on case-insensitive filesystems.

Q78NormalisationHard

See what Path does and does not clean up for you.

Q79Existence LiesHard

Find the cases where an existence check gives a misleading answer.

Q80Path TraversalHardMust Do

Break a naive file server, then fix it.

Q81Illegal NamesHard

Discover which filenames a platform refuses.

Q82Deletion FailuresHard

Handle the deletions that fail halfway through.

Q83Filename EncodingHard

See how filenames are encoded, and where that leaks.

Q84stat() SurprisesHard

Read stat() correctly, including the fields that mean different things.

Q85Race ConditionsHard

Show why check-then-act is unreliable, and what to do instead.

Q86Mini-ProjectMini-Project

Build a Media Organiser that files photos into folders by date.

Q87Mini-ProjectMini-ProjectMust Do

Build a Disk Usage Analyser with a tree chart, like du.

Q88Mini-ProjectMini-Project

Build an Incremental Backup that only stores what changed.

Q89Mini-ProjectMini-Project

Build a Duplicate Cleaner that chooses which copy to keep.

Q90Mini-ProjectMini-Project

Build a Workspace Cleaner that reclaims build artefacts and caches.

Q91Mini-ProjectMini-Project

Build a Static Site Builder that copies, transforms and fingerprints files.

Q92Mini-ProjectMini-ProjectMust Do

Build a Directory Diff tool that reports how two trees differ.

Q93Mini-ProjectMini-Project

Build a Log Archiver that rotates, compresses and expires by policy.

Q94Mini-ProjectMini-Project

Build a Bulk Rename Studio with pattern rules and an undo file.

Q95Mini-ProjectMini-Project

Build a Project Health Report combining size, age, structure and risk.

Q96InterviewInterview

Map pathlib against os and os.path completely, and say when each still wins.

Q97InterviewInterview

State the rules for writing file code that runs on every platform.

Q98InterviewInterview

Set out the safety rules for code that deletes, moves or overwrites files.

Q99InterviewInterview

Measure the cost of every way to walk a tree, and explain the differences.

Q100CapstoneInterviewMust Do

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