OS & Path Operations — Practice Questions
50 questions. Try each one yourself before checking the answer.
Import os and print the current working directory using os.getcwd().
Use os.listdir() to print every file/folder name in the current directory.
Use os.path.exists() to check whether "notes.txt" exists in the current directory.
Use os.path.join() to build a cross-platform path from "documents" and "notes.txt".
Create a file "sample.txt", then use os.path.isfile() and os.path.isdir() to check what kind of thing it is.
Create a Path object for "sample.txt" and check .exists() on it.
Print the current working directory using Path.cwd().
Create a new folder called "demo_folder" using os.makedirs(), with exist_ok=True so it doesn't error if it already exists.
Given a path "documents/reports/summary.pdf", print its base name and its directory name using os.path.basename() and os.path.dirname().
Using pathlib.Path, print .name, .stem, and .suffix for "summary.pdf".
Create a file "old_name.txt", then rename it to "new_name.txt" using os.rename().
Create a temporary file "temp.txt", then delete it using os.remove().
Create "original.txt", then use shutil.copy() to create "copy.txt" with the same content.
Create "demo_folder" (if needed) and use shutil.move() to move "copy.txt" into it.
Use os.path.splitext() to separate the file name from its extension for "report.final.pdf".
Use Path.mkdir(exist_ok=True) to create a folder "pathlib_demo", and demonstrate it doesn't raise an error the second time it's run.
Given a folder with a few .txt files, use Path.glob("*.txt") to list only files matching that pattern.
Print the file size, in bytes, of "sample.txt" using os.path.getsize().
Use Path("sample.txt").stat() to print the file's size via .st_size.
Given the current directory, print separate lists of just the FILES and just the FOLDERS in it, using os.path.isfile()/os.path.isdir().
Build a mini "File Organizer": given a folder of mixed files, move every .txt file into a text_files subfolder.
Find and print every .csv file in the current directory using pathlib.Path.glob().
Calculate the TOTAL size (in bytes) of every file directly inside a given folder.
Create a timestamped-ish backup of "sample.txt" (e.g. "sample_backup.txt") using shutil.copy().
Write a function safe_delete(filename) that only calls os.remove() if the file actually exists, avoiding a crash.
Build a standard project folder structure (src, tests, docs) inside a new my_project folder, using os.makedirs().
Rename every file in a folder to have a "draft_" prefix, using os.rename() in a loop.
Count how many files of each extension exist in a folder, building a dictionary like {".txt": 3, ".csv": 1}.
Find the LARGEST file (by size) in a given folder, printing its name and size.
Use os.walk() to print every file found anywhere INSIDE a folder, including subfolders.
Use os.walk() to print a simple indented tree of a folder's full structure (subfolders and files), using the depth of root relative to the start to control indentation.
Demonstrate shutil.rmtree() to delete an ENTIRE folder tree, with a clear comment warning about how destructive it is.
Use Path.rglob("*.txt") to RECURSIVELY find every .txt file anywhere under a folder, including nested subfolders.
Perform the SAME task (join a folder and filename, then check if it exists) two ways — once with os.path, once with pathlib — side by side.
Demonstrate why hardcoding "folder/file.txt" with a forward slash is risky on Windows, and show the safe cross-platform alternative using os.path.join() or pathlib.
Use os.environ.get() to safely read an environment variable (e.g. "PATH"), providing a default if it's missing.
Use the tempfile module to create a temporary file that's automatically usable for scratch data, without cluttering the project folder.
Use shutil.disk_usage() to print the total, used, and free space (in GB) for the current drive.
Check whether a given Path is a symbolic link using .is_symlink(), and explain in a comment why this check matters before recursively walking a directory tree.
The following code crashes with FileExistsError on its second run. Find and fix the bug.
import os
os.mkdir("shared_folder")Build a "File Organizer Tool" that sorts every file in a folder into subfolders named after their extension (e.g. all .txt files go into a txt/ subfolder).
Build a "Directory Size Analyzer" that recursively computes the total size of an entire folder tree using os.walk().
Build a "Duplicate File Finder" that groups files in a folder by SIZE (a simple heuristic for potential duplicates) and reports any group with more than one file.
Build a "Backup Utility" that copies every file matching a given pattern (e.g. "*.py") from a source folder into a backup/ folder.
Build a "Project Scaffolding Tool" that creates a standard folder layout (src, tests, docs, data) plus starter files (README.md, .gitignore) for a new project name given by the user.
Explain why pathlib is generally preferred over os.path in modern Python code, demonstrating the same multi-step task (build a path, check existence, read content) both ways.
Explain why destructive operations like shutil.rmtree() and os.remove() deserve extra defensive checks in real code, demonstrating a safer wrapper that requires explicit confirmation before deleting.
Explain the difference between os.walk()'s default top-down traversal and bottom-up (topdown=False), and why bottom-up matters if you're deleting empty folders as you go.
Explain a real cross-platform pitfall: manually splitting a path string on "/" breaks on Windows-style paths using "\\". Demonstrate the safe alternative.
Build a "File System Toolkit Report" — the most complete demonstration in this module. Given a project folder, print: total file count (recursive), total size, a breakdown of file counts by extension, and the single largest file — all using pathlib.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session