Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

How to automatically organize your Downloads folder by file type in Python

This script scans the Downloads folder and moves files into sub-folders based on their extensions (e.g., Images, Documents, Videos).

Easy Python 3.9+ Jun 27, 2026 Automation & scripting 1 views 0 copies

Python code

40 lines
Python 3.9+
import os
import shutil
from pathlib import Path

def organize_downloads_folder(downloads_path=None):
    if downloads_path is None:
        downloads_path = str(Path.home() / "Downloads")
    
    if not os.path.exists(downloads_path):
        print(f"Path {downloads_path} does not exist.")
        return
    
    file_categories = {
        "Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
        "Documents": [".pdf", ".docx", ".doc", ".txt", ".pptx"],
        "Videos": [".mp4", ".mkv", ".flv", ".avi"],
        "Music": [".mp3", ".wav", ".flac"],
        "Archives": [".zip", ".tar", ".gz", ".rar"],
    }
    
    for filename in os.listdir(downloads_path):
        file_path = os.path.join(downloads_path, filename)
        if os.path.isfile(file_path):
            file_ext = os.path.splitext(filename)[1].lower()
            moved = False
            for folder_name, extensions in file_categories.items():
                if file_ext in extensions:
                    dest_folder = os.path.join(downloads_path, folder_name)
                    os.makedirs(dest_folder, exist_ok=True)
                    shutil.move(file_path, os.path.join(dest_folder, filename))
                    moved = True
                    break
            if not moved:
                other_folder = os.path.join(downloads_path, "Others")
                os.makedirs(other_folder, exist_ok=True)
                shutil.move(file_path, os.path.join(other_folder, filename))

if __name__ == "__main__":
    organize_downloads_folder()
    print("Downloads folder organized.")

Output

stdout
Downloads folder organized.

How it works

The script uses os.listdir() to iterate over all items in the downloads folder, checking only actual files. Each file's extension is extracted using os.path.splitext() and matched against predefined categories. If a match is found, the destination subfolder (e.g., "Images") is created with os.makedirs(exist_ok=True) and the file is moved with shutil.move(). Files that don't match any category go into an "Others" folder. The code is entirely built on the Python standard library so no dependencies are required.

Common mistakes

  • Forgetting to handle hidden or system files that might cause permission errors.
  • Using `os.rename()` instead of `shutil.move()` which fails when moving across different drives.
  • Not lowercasing the extension before comparison, causing mismatches (e.g., .JPG vs .jpg).

Variations

  1. Use `pathlib.Path.iterdir()` and `pathlib.Path.suffix` for a more modern, readable file iteration.
  2. Add a command-line argument to target a different directory instead of hardcoding Downloads.

Real-world use cases

  • Run as a cron job or scheduled task every night to keep your Downloads folder clutter-free.
  • Integrate into a file sync workflow where new files must be sorted before cloud upload.
  • Use as a starting point for a visual file organizer with a GUI like PyQt or Tkinter.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.