Python · Intermediate Python

OS & Path Operations — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1osEasy

Import os and print the current working directory using os.getcwd().

Q2osEasy

Use os.listdir() to print every file/folder name in the current directory.

Q3os.pathEasy

Use os.path.exists() to check whether "notes.txt" exists in the current directory.

Q4os.pathEasy

Use os.path.join() to build a cross-platform path from "documents" and "notes.txt".

Q5os.pathEasy

Create a file "sample.txt", then use os.path.isfile() and os.path.isdir() to check what kind of thing it is.

Q6pathlibEasy

Create a Path object for "sample.txt" and check .exists() on it.

Q7pathlibEasy

Print the current working directory using Path.cwd().

Q8osEasy

Create a new folder called "demo_folder" using os.makedirs(), with exist_ok=True so it doesn't error if it already exists.

Q9os.pathEasy

Given a path "documents/reports/summary.pdf", print its base name and its directory name using os.path.basename() and os.path.dirname().

Q10pathlibEasy

Using pathlib.Path, print .name, .stem, and .suffix for "summary.pdf".

Q11osMedium

Create a file "old_name.txt", then rename it to "new_name.txt" using os.rename().

Q12osMedium

Create a temporary file "temp.txt", then delete it using os.remove().

Q13shutilMedium

Create "original.txt", then use shutil.copy() to create "copy.txt" with the same content.

Q14shutilMedium

Create "demo_folder" (if needed) and use shutil.move() to move "copy.txt" into it.

Q15os.pathMedium

Use os.path.splitext() to separate the file name from its extension for "report.final.pdf".

Q16pathlibMedium

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.

Q17pathlib.glob()Medium

Given a folder with a few .txt files, use Path.glob("*.txt") to list only files matching that pattern.

Q18os.pathMedium

Print the file size, in bytes, of "sample.txt" using os.path.getsize().

Q19pathlib.stat()Medium

Use Path("sample.txt").stat() to print the file's size via .st_size.

Q20Listing Files vs FoldersMedium

Given the current directory, print separate lists of just the FILES and just the FOLDERS in it, using os.path.isfile()/os.path.isdir().

Q21Real-WorldMedium

Build a mini "File Organizer": given a folder of mixed files, move every .txt file into a text_files subfolder.

Q22Real-WorldMedium

Find and print every .csv file in the current directory using pathlib.Path.glob().

Q23Real-WorldMedium

Calculate the TOTAL size (in bytes) of every file directly inside a given folder.

Q24Real-WorldMedium

Create a timestamped-ish backup of "sample.txt" (e.g. "sample_backup.txt") using shutil.copy().

Q25Real-WorldMedium

Write a function safe_delete(filename) that only calls os.remove() if the file actually exists, avoiding a crash.

Q26Real-WorldMedium

Build a standard project folder structure (src, tests, docs) inside a new my_project folder, using os.makedirs().

Q27Real-WorldMedium

Rename every file in a folder to have a "draft_" prefix, using os.rename() in a loop.

Q28Real-WorldMedium

Count how many files of each extension exist in a folder, building a dictionary like {".txt": 3, ".csv": 1}.

Q29Real-WorldMedium

Find the LARGEST file (by size) in a given folder, printing its name and size.

Q30Real-WorldMedium

Use os.walk() to print every file found anywhere INSIDE a folder, including subfolders.

Q31os.walk()Hard

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.

Q32shutil.rmtree()Hard

Demonstrate shutil.rmtree() to delete an ENTIRE folder tree, with a clear comment warning about how destructive it is.

Q33pathlib.rglob()Hard

Use Path.rglob("*.txt") to RECURSIVELY find every .txt file anywhere under a folder, including nested subfolders.

Q34os.path vs pathlibHard

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.

Q35Cross-Platform PathsHard

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.

Q36os.environHard

Use os.environ.get() to safely read an environment variable (e.g. "PATH"), providing a default if it's missing.

Q37tempfileHard

Use the tempfile module to create a temporary file that's automatically usable for scratch data, without cluttering the project folder.

Q38shutil.disk_usage()Hard

Use shutil.disk_usage() to print the total, used, and free space (in GB) for the current drive.

Q39pathlibHard

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.

Q40DebuggingHard

The following code crashes with FileExistsError on its second run. Find and fix the bug.

import os
 
os.mkdir("shared_folder")
Q41Mini-ProjectMini-Project

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).

Q42Mini-ProjectMini-Project

Build a "Directory Size Analyzer" that recursively computes the total size of an entire folder tree using os.walk().

Q43Mini-ProjectMini-Project

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.

Q44Mini-ProjectMini-Project

Build a "Backup Utility" that copies every file matching a given pattern (e.g. "*.py") from a source folder into a backup/ folder.

Q45Mini-ProjectMini-Project

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.

Q46InterviewInterview

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.

Q47InterviewInterview

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.

Q48InterviewInterview

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.

Q49InterviewInterview

Explain a real cross-platform pitfall: manually splitting a path string on "/" breaks on Windows-style paths using "\\". Demonstrate the safe alternative.

Q50CapstoneInterview

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