By Paul Derrington, Jr.
CEO | Chief Engineer | Research Engineer | Editor, D-CSIL
4 min read
Key Takeaways
A downloads folder sorts itself if you give it rules. This guide writes a short Python script — standard library only, nothing to install — that plans every move in a dry run first, renames instead of overwriting when a name collides, and leaves files it does not recognise exactly where they are.

Why write a sorter instead of asking a chatbot to clean up for you?
You could paste a file list into a chatbot and ask where each file belongs. That works once, for that list. Next month the folder is messy again, the list is different, and you start over — and any file you forgot to paste never gets sorted at all.
A small script buys you the opposite trade: the same rules, applied the same way, every time you run it, over the whole folder, with nothing uploaded anywhere. Your file names never leave your machine. The cost is real too: the script only knows the rules you wrote down. It sorts by file extension, not by what a file is about, and a file type you did not list stays where it is. For a downloads folder, that trade is usually worth it — most of the mess is PDFs, photos, music and spreadsheets, and extension rules cover those fine.
Steps 1–2: Check Python, then write your rules
Step 1: check that Python 3 is installed by running python3 --version in a terminal (on Windows, try py --version). This guide needs nothing else — pathlib and shutil are both in the Python standard library, so there is nothing to install. The workflow below was run and checked on Python 3.12.3 before publishing.
Step 2: write the rules as a dictionary that maps each file extension to a folder name, and save the script below as sort_folder.py. The pathlib documentation notes that a path's suffix is only its final extension — archive.tar.gz has the suffix .gz, not .tar.gz — so list the final extension you actually see. Matching uses suffix.lower(), so photo.JPG and photo.jpg follow the same rule.
Steps 3–4: Dry run first, then protect against overwrites
Step 3: run the script in dry-run mode first. It walks the folder with Path.iterdir(), skips subfolders, skips any file whose extension is not in your rules, and prints every planned move without moving anything. Read the plan. If a rule is wrong, this is the cheap moment to catch it — nothing has moved yet.
Step 4: check the collision rule before any real move. The shutil documentation warns that if the destination already exists and is not a directory, shutil.move may overwrite it, depending on the operating system. The script avoids that entirely: if Documents/report.pdf already exists, the incoming file is renamed to report-2.pdf (then report-3.pdf, and so on) instead of replacing the file that was already there.
Steps 5–6: Move the files, then check what is left
Step 5: flip DRY_RUN to False and run the script again. For each planned move it creates the destination folder if needed and calls shutil.move, which the documentation describes as recursively moving a file or directory to another location and returning the destination; it uses a rename when source and destination are on the same filesystem and falls back to copy-then-remove when they are not.
Step 6: look at what remains in the folder. Anything left should be there on purpose: subfolders, files with no extension, and file types you did not give a rule. In the pre-publish test, a folder holding 7 test files and 1 subfolder sorted exactly that way — 5 files moved (report.pdf, notes.txt, photo.JPG, song.mp3 and data.csv), a name collision safely became report-2.pdf, and mystery.xyz, an extensionless README, and the subfolder were left untouched.
The full script, and its limits
Here is the whole sorter: from pathlib import Path; import shutil; FOLDER = Path.home() / 'Downloads'; RULES = {'.pdf': 'Documents', '.txt': 'Documents', '.jpg': 'Images', '.png': 'Images', '.mp3': 'Audio', '.csv': 'Data'}; DRY_RUN = True. A plan() function collects (source, destination) pairs for files whose suffix.lower() is in RULES; a unique() function appends -2, -3 and so on until the destination name is free; the main loop prints 'DRY:' lines while DRY_RUN is True, and otherwise creates each destination folder with mkdir(exist_ok=True) and calls shutil.move. Change FOLDER to any messy folder — the script does not care that it is Downloads.
Know the limits before you trust it. It does not look inside subfolders, it does not read file contents, and it cannot tell a work PDF from a personal one — both land in Documents. If you need sorting by content or by date, that is a different script. Start with one folder, keep the dry run on until the plan looks right, and only then let it move anything.