Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Computer-Aided Automation (CAA) project using Python

Here’s a Computer-Aided Automation (CAA) project using Python that demonstrates automating a repetitive task in a simple and impactful way.

 


Project: Automated File Organizer

Objective: Automatically organize files in a folder into subfolders based on their file types. For example:

  • Move all PDFs to a "Documents" folder.
  • Move images to an "Images" folder.
  • Move videos to a "Videos" folder.

Features

  1. Detect files in a given directory.
  2. Categorize files based on their extensions.
  3. Move files to corresponding subfolders automatically.

Tools and Libraries

  • Python Libraries:
    • os
      and
      shutil
      for file handling.
    • argparse
      (optional) for command-line input.

Implementation Steps

Step 1: Define File Categories

Create mappings of file extensions to categories:

FILE_CATEGORIES = {
 "Documents": [".pdf", ".docx", ".txt"],
 "Images": [".jpg", ".jpeg", ".png", ".gif"],
 "Videos": [".mp4", ".mov", ".avi"],
 "Audio": [".mp3", ".wav"],
 "Archives": [".zip", ".rar", ".7z"],
 "Others": []
}

Step 2: Script to Organize Files

Here's the full implementation:

import os
import shutil

# Define file categories
FILE_CATEGORIES = {
 "Documents": [".pdf", ".docx", ".txt"],
 "Images": [".jpg", ".jpeg", ".png", ".gif"],
 "Videos": [".mp4", ".mov", ".avi"],
 "Audio": [".mp3", ".wav"],
 "Archives": [".zip", ".rar", ".7z"],
 "Others": []
}

def categorize_file(file_name):
 """Determine the category of a file based on its extension."""
 _, ext = os.path.splitext(file_name)
 for category, extensions in FILE_CATEGORIES.items():
 if ext.lower() in extensions:
 return category
 return "Others"

def organize_files(folder_path):
 """Organize files in the specified folder."""
 if not os.path.exists(folder_path):
 print(f"The folder {folder_path} does not exist.")
 return

 # Iterate over files in the folder
 for file_name in os.listdir(folder_path):
 file_path = os.path.join(folder_path, file_name)
 
 # Skip if it's a directory
 if os.path.isdir(file_path):
 continue
 
 # Categorize the file
 category = categorize_file(file_name)
 category_folder = os.path.join(folder_path, category)
 
 # Create category folder if it doesn't exist
 os.makedirs(category_folder, exist_ok=True)
 
 # Move the file
 shutil.move(file_path, os.path.join(category_folder, file_name))
 print(f"Moved {file_name} to {category}/")

if __name__ == "__main__":
 folder_to_organize = input("Enter the folder path to organize: ").strip()
 organize_files(folder_to_organize)

How It Works

  1. Input: The user specifies a folder path.
  2. Processing:
    • The script iterates through all files in the folder.
    • Files are categorized based on their extensions.
    • Files are moved into corresponding subfolders.
  3. Output: The folder is neatly organized into subfolders like
    Documents
    ,
    Images
    , etc.

Sample Run

Before:

MyFolder/
 invoice.pdf
 picture.jpg
 song.mp3
 video.mp4
 archive.zip

After Running Script:

MyFolder/
 Documents/
 invoice.pdf
 Images/
 picture.jpg
 Audio/
 song.mp3
 Videos/
 video.mp4
 Archives/
 archive.zip

Extensions

  1. Add Logging:
    • Log the actions to a file for audit purposes using Python's
      logging
      module.
  2. Schedule Automation:
    • Use
      cron
      on Linux or Task Scheduler on Windows to run this script periodically.
  3. Advanced Features:
    • Allow the user to customize file categories via a configuration file (e.g., JSON).
  4. GUI:
    • Create a simple interface using
      tkinter
      for non-technical users.

 

caa December 03 2024 5 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 3
Members Online 0

Total Members: 11
Newest Member: Jhilam