File Organizer Automation Project


File Organizer Automation Project in Python

Objective:

Automatically organize files in a specified directory (like Downloads) into categorized folders based on their file types such as documents, images, videos, etc.

Step 1: Import Required Modules

import os
import shutil
from datetime import datetime

Step 2: Define File Type Categories

FILE_TYPES = {
    'Documents': ['.pdf', '.docx', '.txt', '.xls', '.xlsx', '.pptx'],
    'Images': ['.jpg', '.jpeg', '.png', '.gif', '.svg'],
    'Videos': ['.mp4', '.mov', '.avi', '.mkv'],
    'Music': ['.mp3', '.wav', '.aac'],
    'Archives': ['.zip', '.rar', '.tar', '.gz'],
    'Scripts': ['.py', '.js', '.html', '.css'],
    'Executables': ['.exe', '.msi'],
    'Others': []
}

Step 3: Choose the Folder to Organize

SOURCE_FOLDER = "C:/Users/YourName/Downloads"

Step 4: Function to Get Category Based on Extension

def get_category(filename):
    _, ext = os.path.splitext(filename)
    for category, extensions in FILE_TYPES.items():
        if ext.lower() in extensions:
            return category
    return 'Others'

Step 5: File Organization Logic

def organize_files():
    for item in os.listdir(SOURCE_FOLDER):
        item_path = os.path.join(SOURCE_FOLDER, item)

        if os.path.isfile(item_path):
            category = get_category(item)
            category_folder = os.path.join(SOURCE_FOLDER, category)

            if not os.path.exists(category_folder):
                os.makedirs(category_folder)

            # Add timestamp to avoid overwrite if needed
            new_path = os.path.join(category_folder, item)

            # Move file
            shutil.move(item_path, new_path)
            print(f"Moved: {item} → {category}")

Step 6: Create Dated Subfolders (Optional)

def organize_by_date():
    today = datetime.now().strftime('%Y-%m-%d')
    dated_folder = os.path.join(SOURCE_FOLDER, today)

    if not os.path.exists(dated_folder):
        os.makedirs(dated_folder)

    for category in FILE_TYPES.keys():
        cat_path = os.path.join(SOURCE_FOLDER, category)
        if os.path.exists(cat_path):
            shutil.move(cat_path, dated_folder)

Step 7: Run the Organizer

if __name__ == "__main__":
    organize_files()
    # organize_by_date()  # Uncomment if you want daily archiving

Output:

When you run the script, files in the specified directory will be moved into folders like:

Documents/
Images/
Videos/
Others/

Each file will be categorized based on its extension, reducing clutter and boosting productivity.

Bonus: Schedule with Task Scheduler or Cron

You can automate this script to run daily or hourly using:

  • Windows Task Scheduler
  • Linux/macOS Cron Jobs
  • Python schedule module if you want to run from a long-running script