02 – Real-World Python Projects – File Organizer

🎯 Project Objective

To create a Python application that automatically organizes files in a folder based on their type, demonstrating:

  • File and directory management
  • Use of os and shutil modules
  • Loops, conditionals, and functions
  • Practical automation for daily tasks

Project: File Organizer App

Project Description

The File Organizer app scans a directory and moves files into folders based on their file type. For example:

  • Images β†’ Images folder
  • Documents β†’ Documents folder
  • Videos β†’ Videos folder
  • Others β†’ Others folder

This helps keep folders neat and manageable automatically.


Python Example Code

import os
import shutil

# Define file type categories
FILE_TYPES = {
    "Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
    "Documents": [".pdf", ".docx", ".txt", ".xlsx", ".pptx"],
    "Videos": [".mp4", ".mkv", ".mov", ".avi"],
    "Audio": [".mp3", ".wav", ".aac"]
}

# Path to organize
path_to_organize = r"C:\Users\YourName\Downloads"

# Loop through files
for filename in os.listdir(path_to_organize):
    file_path = os.path.join(path_to_organize, filename)
    
    # Skip directories
    if os.path.isdir(file_path):
        continue
    
    # Identify file type
    moved = False
    for folder, extensions in FILE_TYPES.items():
        if filename.lower().endswith(tuple(extensions)):
            folder_path = os.path.join(path_to_organize, folder)
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            shutil.move(file_path, folder_path)
            moved = True
            break
    
    # If file type not recognized
    if not moved:
        others_path = os.path.join(path_to_organize, "Others")
        if not os.path.exists(others_path):
            os.makedirs(others_path)
        shutil.move(file_path, others_path)

print("Files have been organized successfully!")

βœ… Key Features

  • Organizes any folder automatically
  • Categorizes files based on extension
  • Creates folders if they don’t exist
  • Handles unknown file types

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *