Exercise: Downloads Sorter
Finish a command-line tool that sorts a folder's files into Photos, Documents, Installers and Archives, planning the moves first, showing them with --dry-run, and never replacing or deleting a file, while pytest checks each step on folders it builds for itself.
Journeyman · Python · Updated
A Downloads folder fills up with vacation photos, receipts, installers and zip files until nothing can be found in it. You'll finish the Downloads Sorter: a command-line tool that moves each file into a folder for its type, Photos, Documents, Installers or Archives. It plans first, shows the plan with --dry-run, and never puts a file on top of another or deletes one. It works on a practice folder that comes with the exercise, never on your real Downloads folder, and the tests build their own small folders in a temporary place.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-python-downloads-sorter, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:
cd ~/python-course
git clone https://github.com/ask-codey/exercise-python-downloads-sorter.git
Git makes a folder, exercise-python-downloads-sorter, with the exercise code inside. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
On a Mac (zsh):
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
The prompt starts with (.venv) once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.
The folder holds:
sorter.py: the tool, for you to finish.FOLDERS, which says where each type of file goes, is ready.make_practice.py: makespractice-downloads, a practice folder of eleven small files, one of them in a folder namedOld stuff. Ready to use.tests/test_sorter.py: the tests. Each one builds its own small folder in a temporary folder pytest makes (tmp_path), calls your functions, and checks what they return, print and move. You don't need to change it.requirements.txtandpytest.ini: the package the tests need, and where pytest finds the tests and your code.
What to do
The four functions in sorter.py are declared with their names and parameters, and for now each stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should do, with examples. Finish them in order, because each one uses the one before:
category_for(path)returns the folder a file belongs in, looked up inFOLDERSby its extension, such as"Photos"forbeach-day.jpg. Capital letters don't matter (SUNSET.JPGis a photo too), and a type that isn't inFOLDERSgetsNone: it stays where it is.plan_moves(folder)returns the moves that would tidyfolder, as a list of(source, destination)pairs in alphabetical order, where the destination isfolder / category / name. It looks only at the files directly infolder, leaves out a file whose category isNoneor whose destination is already taken, and changes nothing.apply_moves(moves, dry_run)goes through the pairs. Withdry_runtrue it printsWould move beach-day.jpg to Photosfor each and changes nothing; otherwise it makes the folder, moves the file and printsMoved beach-day.jpg to Photos. A destination that's already taken printsSkipped beach-day.jpg: Photos already has oneand moves nothing. It returns how many files moved, or would move.main(argv=None)reads a positional argument,folder, and a--dry-runflag withargparse, then plans the moves and applies them. A folder that isn't there printsNo folder named ...and moves nothing. Its last line isFiles that would move: 9in a dry run, orFiles moved: 9.
Nothing in the tool deletes a file, and nothing should.
Check your work
Run the tests from the exercise folder, with the virtual environment on:
pytest
At first all 17 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back.
To see the tool at work on the practice folder, make the folder, look at the plan, then sort it:
python make_practice.py
python sorter.py practice-downloads --dry-run
python sorter.py practice-downloads
Hints
- Lowercase the extension first.
path.suffixofSUNSET.JPGis.JPG;.lower()turns it into a keyFOLDERShas.FOLDERS.get(...)givesNonefor a key that isn't there. Paths and Folders with pathlib shows a path's parts. - Plan with paths, not text.
folder / category / file.namebuilds the destination, andexists()says whether it's taken.is_file()tells a file from a folder. - The folder's name comes from the destination.
destination.parent.nameisPhotosfor.../Photos/beach-day.jpg, which is what the printed lines need. - Make the folder before the move.
mkdir(parents=True, exist_ok=True)makes it when it's missing and does nothing when it's there. Copying, Moving and Renaming Files Safely shows the checks and the dry run. - Hand
parse_argsthe list.parser.parse_args(argv)reads the tests' list, and the terminal's words whenargvisNone. Command-Line Options with argparse shows the flag.
When you're done
Every test is green: pytest's last line says 17 passed. On the practice folder, the dry run shows the plan and moves nothing:
practice-downloads is ready: 11 files added.
Would move beach-day.jpg to Photos
Would move canyon-hike.JPG to Photos
Would move family-reunion.png to Photos
Would move grocery-receipt.pdf to Documents
Would move hardware-receipt.pdf to Documents
Would move meeting-notes.docx to Documents
Would move packing-list.txt to Documents
Would move photo-editor-setup.exe to Installers
Would move vacation-photos.zip to Archives
Files that would move: 9
Then python sorter.py practice-downloads prints the same nine lines with Moved instead of Would move, and Files moved: 9. budget-2026.xlsx and the Old stuff folder stay where they were.
Now run python make_practice.py again, which puts nine fresh files with the same names in the folder, and run the dry run once more. It says Files that would move: 0. Why does it plan no moves at all, and what would shutil.move have done to the photos already in Photos without your exists() checks?
To run the sorter every week, Running a Script on a Schedule shows the steps.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-python-downloads-sorter), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-python-downloads-sorter.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-python-downloads-sorter, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
A tool that plans before it acts, shows the plan on request and refuses to overwrite is one you can point at a real folder, and the same shape fits every chore you'll automate next.