โ† Back to all guides
๐Ÿ—‚๏ธ

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.

Projectsbeginner6 min read

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()
4
5for category, extensions in FILE_CATEGORIES.items():
6if extension in extensions:
7return category
8
9return 'Other' # Files that don't match any category
10
11
12def organize_folder(folder_path):
13'''Sort all files in a folder into categorized subfolders.'''
14
15# Count how many files we moved
16moved = 0
17
18for filename in os.listdir(folder_path):
19file_path = os.path.join(folder_path, filename)
20
21# Skip folders โ€” we only want to sort files
22if os.path.isdir(file_path):
23continue
24
25# Skip hidden files (like .DS_Store on Mac)
26if filename.startswith('.'):
27continue
28
29# Figure out where this file goes
30category = get_category(filename)
31
32# Create the category folder if it doesn't exist
33category_path = os.path.join(folder_path, category)
34if not os.path.exists(category_path):
35os.makedirs(category_path)
36print(f' Created folder: {category}/')
37
38# Move the file!
39destination = os.path.join(category_path, filename)
40shutil.move(file_path, destination)
41print(f' Moved: {filename} -> {category}/')
42moved += 1
43
44return 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 โœจ