DEV Community

Imran Ahmad
Imran Ahmad

Posted on

๐Ÿ“‚ Automating File Organization with Python ๐Ÿš€

Managing a cluttered Downloads folder is a struggle we all face. Images, documents, videos, executables, and random files pile up quickly, making it difficult to find what you need. Thatโ€™s where Python automation comes to the rescue!

In this blog, Iโ€™ll walk you through how I built a Python File Organizer Script that automatically sorts files into categorized subfolders like Images, Documents, Videos, Audio, Archives, Config, and more.


๐Ÿ“Œ Problem Statement

Create a Python script that automatically organizes files in a folder by file type โ€” putting images, documents, videos, etc., into separate subfolders.


๐Ÿš€ Features of the Script

  • โœ… Automatically detects file types by extension
  • โœ… Creates subfolders if they donโ€™t exist
  • โœ… Moves files into respective folders
  • โœ… Prints logs for confirmation (moved / skipped files)
  • โœ… Easy to customize with new extensions

๐Ÿ› ๏ธ Supported File Types

  • Images โ†’ .jpg, .jpeg, .png, .gif
  • Documents โ†’ .pdf, .docx, .txt, .xlsx, .pptx
  • Videos โ†’ .mp4, .avi, .mkv
  • Audio โ†’ .mp3, .wav
  • Archives โ†’ .zip, .rar
  • Scripts โ†’ .py, .js, .md
  • Config โ†’ .ini
  • Applications โ†’ .exe

๐Ÿ“œ Python Code

Hereโ€™s the complete script:

import os
import shutil

def organize_folder(folder_path):
    # Define file type categories
    file_types = {
        'Images': ['.jpg', '.jpeg', '.png', '.gif'],
        'Documents': ['.pdf', '.docx', '.txt', '.xlsx', '.pptx'],
        'Videos': ['.mp4', '.avi', '.mkv'],
        'Audio': ['.mp3', '.wav'],
        'Archives': ['.zip', '.rar'],
        'Scripts': ['.py', '.js', '.md'],
        'Config': ['.ini'],
        'Applications': ['.exe']
    }

    print(f"๐Ÿ“‚ Organizing files in: {folder_path}\n")

    files_moved = 0

    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)

        if os.path.isfile(file_path):
            _, ext = os.path.splitext(filename)
            moved = False

            for folder, extensions in file_types.items():
                if ext.lower() in extensions:
                    target_folder = os.path.join(folder_path, folder)
                    os.makedirs(target_folder, exist_ok=True)
                    shutil.move(file_path, os.path.join(target_folder, filename))
                    print(f"โœ… Moved: {filename} โ†’ {folder}/")
                    files_moved += 1
                    moved = True
                    break

            if not moved:
                print(f"โš ๏ธ Skipped (Unknown type): {filename}")

    print(f"\nโœจ Done! Total files moved: {files_moved}")

# Run the script
organize_folder(r"C:\\Users\\imran\\Downloads")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ธ Example Output

๐Ÿ“‚ Organizing files in: C:\Users\imran\Downloads

โœ… Moved: photo.jpg โ†’ Images/
โœ… Moved: resume.docx โ†’ Documents/
โœ… Moved: movie.mp4 โ†’ Videos/
โš ๏ธ Skipped (Unknown type): setup.tmp

โœจ Done! Total files moved: 5
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Why This Script is Useful?

  • Keeps your Downloads folder neat and tidy
  • Saves time searching for files
  • Boosts productivity with automation
  • Works on Windows, Mac, and Linux

๐Ÿ”ฎ Future Improvements

  • Add an Others folder for unknown file types
  • Schedule script with Task Scheduler (Windows) or Cron Jobs (Linux/Mac)
  • Add a GUI interface for non-technical users

๐Ÿ™Œ Final Thoughts

This project is a simple yet powerful example of how Python automation can simplify everyday tasks. With just a few lines of code, you can turn a messy folder into a well-organized one.

๐Ÿ’ก If you want to try it out, check out the GitHub repo here ๐Ÿ‘‰ python-file-organizer-automation


โœ๏ธ Author: Imran Ahmad

๐Ÿš€ Passionate about Python Automation, Selenium, and scripting for productivity.

Top comments (0)