Mini Project: File Organizer
Build a CLI tool that sorts files into folders by extension
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
What you are building
A file organizer that takes a directory path and sorts its files into subfolders by extension. Running it on a messy Downloads folder creates folders like pdf/, jpg/, txt/, and moves each file to the matching folder.
This project combines everything from this lesson:
- pathlib — scan directories and build paths
- argparse — accept the target directory as a CLI argument
- os — create subdirectories
The algorithm
- Accept a directory path from the command line
- List all files in that directory (skip subdirectories)
- For each file, get its extension (e.g.,
.pdf→pdf) - Create a subdirectory named after the extension (if it doesn't exist)
- Print a message for each file showing where it would move
For safety, your organizer will print the planned moves instead of actually moving files. This is a common pattern in CLI tools — a "dry run" mode that shows what would happen.
Your task
Build the organize_files function and the CLI interface. The function takes a directory path string, scans it, and prints the move plan. The argparse setup accepts the directory as a positional argument.
You will use parse_args(["test_folder"]) to simulate running the command with a test directory — this lets you test the argument parsing in the code editor.
Instructions
Build the file organizer.
- Define
organize_files(directory). Create a variable namedpathand assign itPath(directory). - Inside the function, create a variable named
filesand assign it a list comprehension:[f for f in path.iterdir() if f.is_file()]. - Inside the function, create a variable named
extensionsand assign itCounter(f.suffix.lower() for f in files if f.suffix). - Inside the function, loop through
fileswith loop variablefile. Inside the loop, iffile.suffixis truthy, create a variable namedext_folderand assign itfile.suffix[1:].lower(). Callprint()withf" {file.name} -> {ext_folder}/{file.name}". - Call
print()withf"\nSummary: {len(files)} files". Loop throughextensions.most_common()with loop variableext, count. Inside the loop, callprint()withf" {ext}: {count} files". - After the function definition, import
argparse. Create a variable namedparserand assign itargparse.ArgumentParser(description="Organize files by extension"). Callparser.add_argument("directory", help="Directory to organize"). - Create a variable named
argsand assign itparser.parse_args(["test_folder"]). - Call
print()withf"Organizing: {args.directory}". Callorganize_files(args.directory).
from pathlib import Path import os from datetime import datetime, date, timedelta from collections import Counter, defaultdict, namedtuple # Step 1: Define organize_files(directory), create path variable # Step 2: Get list of files (not directories) # Step 3: Count extensions with Counter # Step 4: Loop through files, print move plan for each # Step 5: Print summary with file count and extension breakdown # Step 6: Set up argparse with "directory" argument # Step 7: Parse test arguments # Step 8: Print the directory and call organize_files
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding