DEV Community

Cover image for Automated Download Folder Organization with python
Samuel Oyerinde
Samuel Oyerinde

Posted on

1

Automated Download Folder Organization with python

The Problem

Lately, my Downloads folder had become a chaotic mess. Finding a single file meant endless scrolling and searching—frustrating and time-consuming. But then, I stumbled upon an article on Dev.to about Python automation, which sparked an idea. After some research, I wrote a script that automates the organization of my Downloads folder.

The Solution

The script performs the following tasks:

  • Scans my Downloads folder
  • Identifies file types (e.g., PDFs, images, videos, etc.)
  • Moves each file into its designated folder

Here's a visual representation of how the organized folder structure looks:

Image description

The Code

Step 1: Check Folder Existence and Create Folders

First, we need to ensure the specified Downloads folder exists and create the necessary subfolders if they don't already exist.

import os
import shutil

# Define file types and their corresponding folders
FILE_TYPES = {
    "Documents": ['.pdf', '.docx', '.txt'],
    "Images": ['.jpg', '.png', '.gif'],
    "Videos": ['.mp4', '.mkv', '.avi'],
    "Other": []  # Default folder for unknown file types
}

def organize_downloads(download_folder):
    # Check if the folder exists
    if not os.path.exists(download_folder):
        print(f"The folder {download_folder} does not exist.")
        return

    # Create folders if they don't exist
    for folder in FILE_TYPES.keys():
        folder_path = os.path.join(download_folder, folder)
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
Enter fullscreen mode Exit fullscreen mode

Step 2: Organize Files

Next, we iterate through all files in the Downloads folder, identify their types, and move them to their respective folders.

    # Iterate through files in the download folder
    for file in os.listdir(download_folder):
        file_path = os.path.join(download_folder, file)

        # Skip directories
        if os.path.isdir(file_path):
            continue

        file_extension = os.path.splitext(file)[1].lower()

        # Find the right folder for the file
        destination_folder = "Other"
        for folder, extensions in FILE_TYPES.items():
            if file_extension in extensions:
                destination_folder = folder
                break

        # Move the file to the appropriate folder
        destination_path = os.path.join(download_folder, destination_folder, file)
        shutil.move(file_path, destination_path)
        print(f"Moved {file} to {destination_folder}")

# Call and run the script
if __name__ == "__main__":
    download_folder = input("Enter the path to your downloads folder: ")
    organize_downloads(download_folder)
    print("Download folder organized successfully!")
Enter fullscreen mode Exit fullscreen mode

What’s Next?

I’m now working on enhancing this script to run automatically based on specific triggers (e.g., time intervals or folder size). The goal is to make file organization completely hands-off, so my system stays organized without any manual intervention.

Share Your Thoughts!

If you’re curious about the code or want to try it out yourself, feel free to adapt it to your needs or suggest improvements—I’d love to hear your thoughts! You can find the code on GitHub: https://github.com/sam4rano/python-automation.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it