Automated File Organizer

 import os

import shutil


# Define file categories with their extensions

FILE_CATEGORIES = {

    "Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg"],

    "Documents": [".pdf", ".docx", ".txt", ".xlsx", ".pptx", ".csv"],

    "Videos": [".mp4", ".avi", ".mkv", ".mov"],

    "Music": [".mp3", ".wav", ".flac"],

    "Archives": [".zip", ".rar", ".tar", ".gz"],

    "Programs": [".exe", ".msi", ".dmg"],

    "Others": []

}


def organize_files(folder_path):

    """Organizes files into categorized folders based on extensions."""

    

    if not os.path.exists(folder_path):

        print(f"Error: The folder '{folder_path}' does not exist.")

        return

    

    # Create folders if they don't exist

    for category in FILE_CATEGORIES.keys():

        category_path = os.path.join(folder_path, category)

        os.makedirs(category_path, exist_ok=True)


    # Iterate through all files in the folder

    for file_name in os.listdir(folder_path):

        file_path = os.path.join(folder_path, file_name)

        

        # Skip directories

        if os.path.isdir(file_path):

            continue

        

        # Get file extension

        file_ext = os.path.splitext(file_name)[1].lower()


        # Determine the correct category for the file

        destination_folder = "Others"  # Default category

        for category, extensions in FILE_CATEGORIES.items():

            if file_ext in extensions:

                destination_folder = category

                break


        # Move the file to the correct folder

        shutil.move(file_path, os.path.join(folder_path, destination_folder, file_name))

        print(f"Moved: {file_name} → {destination_folder}")


    print("✅ File organization completed successfully!")


# Run the script

if __name__ == "__main__":

    folder_to_organize = input("Enter the folder path to organize: ")

    organize_files(folder_to_organize)


No comments: