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
- Detect files in a given directory.
- Categorize files based on their extensions.
- Move files to corresponding subfolders automatically.
Tools and Libraries
- Python Libraries:
andos
for file handling.shutil
(optional) for command-line input.argparse
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
- Input: The user specifies a folder path.
- Processing:
- The script iterates through all files in the folder.
- Files are categorized based on their extensions.
- Files are moved into corresponding subfolders.
- Output: The folder is neatly organized into subfolders like
,Documents
, etc.Images
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
- Add Logging:
- Log the actions to a file for audit purposes using Python's
module.logging
- Log the actions to a file for audit purposes using Python's
- Schedule Automation:
- Use
on Linux or Task Scheduler on Windows to run this script periodically.cron
- Use
- Advanced Features:
- Allow the user to customize file categories via a configuration file (e.g., JSON).
- GUI:
- Create a simple interface using
for non-technical users.tkinter
- Create a simple interface using
No Comments have been Posted.