Automate Your Messy Downloads Folder with Python (Beginner Script) 📂

Automate Your Messy Downloads Folder with Python

⚡ What You Will Build

  • The Problem: Your Downloads folder is a mess of 1,000 random files.
  • The Fix: A Python script that instantly sorts them into folders like “Images,” “PDFs,” and “Installers.”
  • The Skills: You will learn the os and shutil libraries.

Open your Downloads folder right now. Be honest—how bad is it?

If you are like most people, it’s a graveyard of old screenshots, PDF menus, and random setup.exe files. Today, we are going to write a “janitor script” that cleans it up for you in less than 1 second.


The Logic: How It Works 🧠

The script follows a simple 3-step logic:

  1. Look at every file in the folder.
  2. Check the extension (e.g., is it a .jpg or a .pdf?).
  3. Move it to the correct home (e.g., move .jpg to the “Images” folder).

The Code 💻

Open VS Code and create a new file named cleaner.py. Copy and paste this code:

import os
import shutil

# 1. Define the folder to clean (Change this to your actual path!)
# Windows example: r"C:\Users\YourName\Downloads"
# Mac example: "/Users/YourName/Downloads"
DOWNLOADS_PATH = r"/Users/YourName/Downloads"

# 2. Define the rules (Extension -> Folder Name)
EXTENSION_MAP = {
    ".jpg": "Images",
    ".png": "Images",
    ".jpeg": "Images",
    ".gif": "Images",
    ".pdf": "Documents",
    ".docx": "Documents",
    ".txt": "Documents",
    ".zip": "Archives",
    ".rar": "Archives",
    ".exe": "Software",
    ".dmg": "Software",
    ".mp4": "Videos"
}

def clean_folder():
    # Loop through every file in the folder
    for filename in os.listdir(DOWNLOADS_PATH):
        file_path = os.path.join(DOWNLOADS_PATH, filename)

        # Skip if it's a folder (we only want to move files)
        if os.path.isdir(file_path):
            continue

        # Get the file extension (e.g., ".jpg")
        _, extension = os.path.splitext(filename)
        extension = extension.lower()

        # Check if we have a rule for this extension
        if extension in EXTENSION_MAP:
            folder_name = EXTENSION_MAP[extension]
            folder_path = os.path.join(DOWNLOADS_PATH, folder_name)

            # Create the folder if it doesn't exist
            os.makedirs(folder_path, exist_ok=True)

            # Move the file
            shutil.move(file_path, os.path.join(folder_path, filename))
            print(f"Moved {filename} -> {folder_name}")

if __name__ == "__main__":
    clean_folder()
    print("Cleanup Complete! 🧹")

How to Customize It ⚙️

1. Set Your Path

The most important line is DOWNLOADS_PATH. You need to put the actual path to your computer’s folder.

  • Windows Tip: Use an r before the quote (e.g., r"C:\Users...") to fix backslash issues.
  • Mac/Linux Tip: Use forward slashes (e.g., "/Users/...").

2. Add More Rules

Do you work with Excel files? Add this to the EXTENSION_MAP:

".xlsx": "Spreadsheets",
".csv": "Spreadsheets",

Why `os` and `shutil`?

You will see these two libraries in almost every Python automation script.

  • os (Operating System): Lets Python “talk” to your computer. It handles paths, folders, and file names.
  • shutil (Shell Utilities): This is the “heavy lifter.” It handles moving, copying, and deleting files.

Frequently Asked Questions

Will this delete my files?

No. shutil.move only moves files. It does not delete them. However, it is always smart to backup important files before running a new script.

What happens to files with no extension?

The script ignores them. They will stay in the main folder.

Can I run this automatically every day?

Yes! On Wednesday, we will write a guide on how to schedule scripts to run automatically so you never have to organize files manually again.

Want this to run automatically? Check out our guide on How to Schedule Python Scripts.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top