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).
Python code
40 linesimport 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
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
- Use `pathlib.Path.iterdir()` and `pathlib.Path.suffix` for a more modern, readable file iteration.
- 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
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
Keep learning
Related tutorials and quizzes for this topic.