What you end up with
Downloads\
Archives\ photos.zip
Documents\ notes.txt, report.xlsx, Lease agreement.pdf
Images\ screenshot.png
Installers\ setup-7z.exe
Video\ video.MP4
song.mp3 its extension isn't in the dictionary — left in place
Folders appear only for the types that were found. The script doesn’t touch subfolders in Downloads.
Step by step
Write the script
The file
sort_downloads.pyin your scripts folder:from pathlib import Path DOWNLOADS = Path.home() / "Downloads" DRY_RUN = True # True — just print the plan; False — move the files FOLDERS = { ".jpg": "Images", ".jpeg": "Images", ".png": "Images", ".webp": "Images", ".pdf": "Documents", ".docx": "Documents", ".xlsx": "Documents", ".txt": "Documents", ".zip": "Archives", ".7z": "Archives", ".rar": "Archives", ".exe": "Installers", ".msi": "Installers", ".mp4": "Video", ".mov": "Video", } moved = 0 for item in DOWNLOADS.iterdir(): if not item.is_file(): continue # leave folders alone folder = FOLDERS.get(item.suffix.lower()) if folder is None: print("keeping:", item.name) continue target = DOWNLOADS / folder / item.name if target.exists(): print("already in", folder + ":", item.name) continue print(item.name, "->", folder) if not DRY_RUN: target.parent.mkdir(exist_ok=True) item.rename(target) moved += 1 if DRY_RUN: print("Dry run: would move", moved, "files. For the real thing, set DRY_RUN = False") else: print("Files moved:", moved)The folder names are yours to choose: change the
FOLDERSdictionary however you like.The dry run
python sort_downloads.py # already in Images: IMG_2041.jpg # notes.txt -> Documents # photos.zip -> Archives # report.xlsx -> Documents # screenshot.png -> Images # setup-7z.exe -> Installers # video.MP4 -> Video # Lease agreement.pdf -> Documents # keeping: song.mp3 # Dry run: would move 7 files. For the real thing, set DRY_RUN = FalseRead the whole plan. If something goes to the wrong place, fix the dictionary and run again: the files are still where they were.
The real run
In the script, change
DRY_RUN = TruetoDRY_RUN = Falseand run it again:python sort_downloads.py # … # Files moved: 7Then set it back to
True, so the next run starts with a plan again.
What’s new here
- Paths:
pathlib.Path.home()is the home folder, and/joins path parts:Path.home() / "Downloads"givesC:\Users\…\Downloadson Windows and/home/…/Downloadson Linux. A path has properties and actions:.nameis the file name,.suffixthe extension with the dot, plus.exists(),.is_file(),.rename(). - A dictionary.
FOLDERSholds “key: value” pairs: an extension and its folder.FOLDERS.get(".pdf")returns"Documents", and for an extension that isn’t there,None, with no error. - A
forloop.for item in DOWNLOADS.iterdir():runs the body for each file and folder inside Downloads;itembecomes each of them in turn.continuegoes straight to the next one. - An
ifcondition. The body runs only if the condition is true:if target.exists():,if not DRY_RUN:.is Nonechecks that there’s no value.
moved += 1 is short for moved = moved + 1: it counts how many files were moved.
Why a dry run
A script that moves files fails silently: a typo in a path and the files go somewhere else. A plan on the screen catches
that before anything moves. The same trick on a server — An irreversible command gets a dry run first. Overwriting is ruled out too: on Windows rename
fails if a file with that name already exists, while on Linux it silently replaces it — that’s why the target.exists()
check comes before the move.
If it doesn’t work
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\Users\…\Downloads'
- Cause
- Downloads was moved to another drive or to OneDrive:
Path.home() / "Downloads"points where it isn’t. - Fix
- In the Downloads folder’s Properties, the Location tab has the real path; write it in:
DOWNLOADS = Path(r"D:\Downloads").
A .JPG file stayed put, though .jpg is in the dictionary
- Cause
- The extension is compared as text, and
.JPGand.jpgare different strings. - Fix
- The script already has
.lower():item.suffix.lower(). If you removed it, put it back.
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
- Cause
- The file is still downloading or open in a program: Windows won’t move an open file.
- Fix
- Close the program or wait for the download to finish and run the script again: it won’t see the files already moved.
The dry run prints “keeping” for almost everything
- Cause
- Those files’ extensions aren’t in the
FOLDERSdictionary. - Fix
- Add lines to the dictionary —
".mp3": "Music",— and do another dry run.
How to check
The dry run prints a plan, and nothing in Downloads changes. After the real run the files are in their folders, and the number in the “Files moved” line matches the number of lines with an arrow in the plan.