Blog Logo

Oct 22 2024 ~ 3 min read

No More Mess: Organizing Folder with a Python Script


Image for file-organizer-diff.webp

My downloads folder is always a mess, and gradually, my desktop started to suffer the same fate. Every time I open it, I feel an overwhelming urge to close it and run away. I tried organizing things manually, but it quickly became a tedious chore. Then, an idea struck me—why not automate it with a simple Python script? Python is such a powerful and versatile tool.

So, I created a script to automatically organize my files, and it worked like a charm. Now, with just a single run, everything is neatly sorted into categories. Feel free to use this script for your own organization needs. Just replace the folder path with yours and add any additional file extensions you want to include.

I’ve also uploaded the script to my GitHub for easy access. Who knows, maybe I’ll start a series where I share useful code snippets or scripts that solve everyday problems. Stay tuned!

Here’s the Python script I used:

import os
import shutil

folder_path = '/Users/samarthgoudar/Downloads'

file_types = {
    'Images': ['.jpg', '.png', '.gif', '.jpeg', '.heic', '.webp'],
    'Documents': ['.pdf', '.docx', '.txt', '.xlsx', '.epub', '.doc'],
    'Videos': ['.mp4', '.mkv', '.mov'],
    'Music': ['.mp3', '.wav'],
    'Archives': ['.zip', '.tar', '.gz'],
    'Data': ['.csv', '.json'],
    'Diagrams': ['.drawio', '.svg', '.excalidraw']
}

def organize_folder():
    for filename in os.listdir(folder_path):
        file_ext = os.path.splitext(filename)[1].lower()
        for folder, extensions in file_types.items():
            if file_ext in extensions:
                folder_path = os.path.join(folder_path, folder)
                if not os.path.exists(folder_path):
                    os.makedirs(folder_path)
                shutil.move(os.path.join(folder_path, filename), folder_path)
                print(f'Moved: {filename} -> {folder}')
                break

organize_folder()

How to Use:

Update the folder_path with the path to your downloads or desktop folder.

folder_path = '/Users/samarthgoudar/Downloads'

Add any additional file_types and extensions you want to sort under the file_types dictionary.

file_types = {
    'Images': ['.jpg', '.png', '.gif', '.jpeg', '.heic', '.webp'],
    'Documents': ['.pdf', '.docx', '.txt', '.xlsx', '.epub', '.doc'],
    'Videos': ['.mp4', '.mkv', '.mov'],
    'Music': ['.mp3', '.wav'],
    'Archives': ['.zip', '.tar', '.gz'],
    'Data': ['.csv', '.json'],
    'Diagrams': ['.drawio', '.svg', '.excalidraw']
}

Run the script, and watch your files magically get organized into their respective categories!

Another easy enhancement you could make is to schedule the Python script to run automatically once a day. This way, you won’t have to remember to manually run the script every time your folder starts getting messy. For example, if you’re on a macOS or Linux system, you can use a cron job to schedule it. On Windows, you can use Task Scheduler. With this simple addition, your downloads folder will stay organized effortlessly, freeing you from the constant clutter and letting the script handle everything in the background.

Check out the full script on my GitHub. Link

You may also like


Headshot of Samarth

Hi, I'm Samarth. I'm a software engineer based in Los Angeles. You can follow me on Twitter, see some of my work on GitHub, or read more about me on LinkedIn.