DEV Community

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

Posted on

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.

Top comments (0)