Introduction
If your Downloads folder is full of mixed files, finding anything takes far too long. In this post, you will build a small Python script that sorts files by type into clean folders — Images, Documents, Videos, Music, and Others. No paid tool is needed, and no extra library needs to be installed — only Python's standard library.
Quick answer: Use os and shutil to read each file's extension, create a folder for that type if it does not exist yet, and move the file there with shutil.move(). The full code is below.
Finished result first: After running, the test folder will contain Images/, Documents/, Videos/, Music/, and Others/, with every test file in its correct category folder.
Prerequisites: Python installed, basic terminal use, and a test folder. No library installation is required.
How to organize folders with Python — step by step
- Choose a folder — try a test folder first, never run this directly on your real Downloads folder first.
- Read each file's extension, for example
.jpg,.pdf, or.mp4. - Map the extension to a category.
- Create the category folder if it does not exist yet.
- Move the file. Any type that is not in the map goes to Others.
Full working code
import os
import shutil
# Run on a TEST folder first, on the real folder only later
FOLDER = "test_downloads"
CATEGORIES = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".webp"],
"Documents": [".pdf", ".docx", ".txt", ".xlsx", ".pptx"],
"Videos": [".mp4", ".mkv", ".mov"],
"Music": [".mp3", ".wav"],
}
def organize(folder):
for filename in os.listdir(folder):
path = os.path.join(folder, filename)
if not os.path.isfile(path):
continue
ext = os.path.splitext(filename)[1].lower()
category = "Others"
for name, extensions in CATEGORIES.items():
if ext in extensions:
category = name
break
target_dir = os.path.join(folder, category)
os.makedirs(target_dir, exist_ok=True)
shutil.move(path, os.path.join(target_dir, filename))
print(f"{filename} -> {category}/")
organize(FOLDER)
print("Done!")
Run this code: In the terminal, run python organize.py — first create a folder called test_downloads and put 4-5 test files in it, then check the result.
Expected output (terminal):
photo.jpg -> Images/
notes.pdf -> Documents/
song.mp3 -> Music/
Done!
Common mistakes
- Running it directly on the real Downloads folder: Always test first. A moved file is hard to find again if you did not expect the move.
- Treating folders as files: That is why the code checks
os.path.isfile()— folders are skipped. - Extension case:
.JPGand.jpgare different, so.lower()is essential.
What if it fails?
- FileNotFoundError: Check the folder name and path — is
test_downloadsin the same place as the script? - PermissionError: Close the file if it is open in another app, then run again.
- Some files went to Others: That is expected behaviour — their extension was not in
CATEGORIES. Add a new category for them.
FAQ
Q: Does this delete files?
No. It only moves files, it does not delete them. Still test on a test folder first.
Q: How do I add a new type?
Add a line to CATEGORIES, for example "Archives": [".zip", ".rar"].
Q: Can this run every day automatically?
Yes, but that is a later step — first understand this version, then scheduling will be covered in a later post.
Conclusion
One small script makes a big difference: a messy folder now cleans itself. Change the code to fit your needs, add new categories, and practise on a test folder.
See more videos on YouTube: https://youtube.com/@pyblitz — Subscribe for more practical Python projects, explained step by step.
💬 Comments