Organize Your Messy Desktop with Python
Write a Python script that automatically sorts your desktop files into folders. Never look at 200 scattered screenshots again.
Organize Your Messy Desktop with Python
Be honest. How many random files are on your desktop right now? Screenshots from three months ago? That PDF you downloaded once? A folder called "New Folder (3)"? Let's write a Python script that cleans that mess up automatically.
What We're Building
A script that:
- Scans your desktop (or any folder)
- Sorts files into organized folders by type (Images, Documents, Music, etc.)
- Runs in seconds
- Can be scheduled to run automatically
๐ฌ Denise says
This was one of the first Python scripts I ever wrote that felt USEFUL. Not a tutorial exercise, not a fake project โ something that saved me actual time. After running it once I was like "wait, coding can just... solve problems in my life?" Yes. Yes it can.
Step 1: The Plan
Here's how files will get sorted:
1
Step 2: Map File Types to Folders
1
Step 3: The Sorting Function
1def get_category(filename):2'''Figure out which folder a file belongs in based on its extension.'''3extension = os.path.splitext(filename)[1].lower()45for category, extensions in FILE_CATEGORIES.items():6if extension in extensions:7return category89return 'Other' # Files that don't match any category101112def organize_folder(folder_path):13'''Sort all files in a folder into categorized subfolders.'''1415# Count how many files we moved16moved = 01718for filename in os.listdir(folder_path):19file_path = os.path.join(folder_path, filename)2021# Skip folders โ we only want to sort files22if os.path.isdir(file_path):23continue2425# Skip hidden files (like .DS_Store on Mac)26if filename.startswith('.'):27continue2829# Figure out where this file goes30category = get_category(filename)3132# Create the category folder if it doesn't exist33category_path = os.path.join(folder_path, category)34if not os.path.exists(category_path):35os.makedirs(category_path)36print(f' Created folder: {category}/')3738# Move the file!39destination = os.path.join(category_path, filename)40shutil.move(file_path, destination)41print(f' Moved: {filename} -> {category}/')42moved += 14344return moved
Step 4: Make It Interactive
1
โ ๏ธ Heads up
Test this on a folder with copies first before running it on your actual Desktop! Create a test folder with some dummy files to make sure it works the way you want. Once you're confident, unleash it on the real mess.
Step 5: Add an Undo Feature
Because sometimes you want your mess back:
1
Level Up Ideas
- Schedule it โ use cron (Mac/Linux) or Task Scheduler (Windows) to run it daily
- Sort by date โ create subfolders like Images/2025-04/ based on when files were created
- Smart rename โ rename screenshots from "IMG_4521.png" to "screenshot-2025-04-20.png"
- Desktop wallpaper โ randomly set your wallpaper from your Images folder
- Size report โ show how much space each category takes up
๐ฌ Denise says
You just wrote a real automation script. This is exactly the kind of thing Python developers do at their jobs โ automate repetitive tasks that nobody wants to do by hand. You could put this on your portfolio, show it in an interview, or just enjoy having a clean desktop for once. All three are valid.
Photo coming soon โจ
Want to keep going?
Tell me what you want to build next and I'll help you write the code.
Start Building โจ