Project: FileMagic_Organizer

This commit is contained in:
Vinayak Mali 2023-10-13 16:05:06 +05:30 committed by GitHub
parent 8aa865cff9
commit 2025bd9507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,61 @@
## FileMagic Organizer - Unleash the Power of Order
Welcome to FileMagic Organizer, where chaos meets its match. This Python script is your ally in the battle against cluttered directories. FileMagic Organizer effortlessly sorts your files into designated categories, bringing harmony to the digital realm.
## Index
- [Introduction](#)
- [Key Features](#key-features)
- [Getting Started](#getting-started)
- [Options provided](#options-provided)
- [Usage](#usage)
- [Customization](#customization)
## Key Features:
- 🔮 **Magical Categorization:** FileMagic Organizer intelligently sorts files by type, creating a structured and easily navigable file system.
- 📅 **Chronological Arrangement:** Dive into the past with chronological organization, unveiling the history of your files in an orderly timeline.
📏 **Size-based Sorting:** Experience the wizardry of size-based categorization, with files neatly grouped into small, medium, and large categories.
## **Getting Started:**
1. ⚡ **Clone the Repository:** Embrace the magic by cloning the FileMagic Organizer repository.
2. 🚀 **Install Dependencies:** Initiate the enchantment with a quick installation of the required Python libraries.
3. ✨ **Run the Magic Spell:** Execute the script, follow the prompts, and witness the transformation as FileMagic Organizer weaves its organizational magic.
## Options provided
- Organize files by type (e.g., images, documents, videos).
- Organize files by creation date into a hierarchical structure of year and month.
- Categorize files by size into small, medium, and large categories.
## Usage
1. **Clone the Repository:**
```bash
git clone https://github.com/malivinayak/file-organizer.git
cd file-organizer
```
2. **Install Dependencies:**
Ensure you have Python installed. Install the required libraries:
```bash
pip install -r requirements.txt
```
3. **Run the Script:**
```bash
python organize_files.py
```
## Customization
- Adjust the file type categories, file extensions, and any other settings in the script based on your needs.
- Modify the size categories and ranges in the script for organizing files by size.

104
FileMagic_Organizer/main.py Normal file
View File

@ -0,0 +1,104 @@
import os
import shutil
import datetime
def categorize_by_size(file_size):
# Define size categories and their ranges in bytes
size_categories = {
'small': (0, 1024), # Up to 1 KB
'medium': (1025, 1024 * 1024), # 1 KB to 1 MB
'large': (1024 * 1025, float('inf')) # Larger than 1 MB
}
for category, (min_size, max_size) in size_categories.items():
if min_size <= file_size < max_size:
return category
return 'unknown'
def organize_files(source_dir, destination_dir, organize_by_type=True, organize_by_date=True, organize_by_size=True):
# Create a dictionary to map file extensions to corresponding folders
file_types = {
'images': ['.png', '.jpg', '.jpeg', '.gif'],
'documents': ['.pdf', '.docx', '.txt'],
'videos': ['.mp4', '.avi', '.mkv'],
'other': [] # Add more categories and file extensions as needed
}
# Create destination subdirectories if they don't exist
if organize_by_type:
for folder in file_types:
folder_path = os.path.join(destination_dir, folder)
os.makedirs(folder_path, exist_ok=True)
if organize_by_date:
for year in range(2010, 2030): # Adjust the range based on your needs
year_folder = os.path.join(destination_dir, str(year))
os.makedirs(year_folder, exist_ok=True)
for month in range(1, 13):
month_folder = os.path.join(year_folder, f"{month:02d}")
os.makedirs(month_folder, exist_ok=True)
if organize_by_size:
for size_category in ['small', 'medium', 'large']:
size_folder = os.path.join(destination_dir, size_category)
os.makedirs(size_folder, exist_ok=True)
# Scan the source directory and organize files
for filename in os.listdir(source_dir):
file_path = os.path.join(source_dir, filename)
if os.path.isfile(file_path):
# Determine the file type based on extension
file_type = None
for category, extensions in file_types.items():
if any(filename.lower().endswith(ext) for ext in extensions):
file_type = category
break
if organize_by_type and file_type:
# Move the file to the corresponding subdirectory
destination_folder = os.path.join(destination_dir, file_type)
destination_path = os.path.join(destination_folder, filename)
shutil.move(file_path, destination_path)
print(f"Moved: {filename} to {file_type} folder")
if organize_by_date:
# Get the creation date of the file
creation_time = os.path.getctime(file_path)
creation_date = datetime.datetime.fromtimestamp(creation_time)
# Determine the destination folder based on creation date
destination_folder = os.path.join(
destination_dir,
str(creation_date.year),
f"{creation_date.month:02d}",
)
# Move the file to the corresponding subdirectory
destination_path = os.path.join(destination_folder, filename)
shutil.move(file_path, destination_path)
print(f"Moved: {filename} to {creation_date.year}/{creation_date.month:02d} folder")
if organize_by_size:
# Get the size of the file in bytes
file_size = os.path.getsize(file_path)
# Determine the destination folder based on file size
size_category = categorize_by_size(file_size)
destination_folder = os.path.join(destination_dir, size_category)
destination_path = os.path.join(destination_folder, filename)
shutil.move(file_path, destination_path)
print(f"Moved: {filename} to {size_category} folder")
# Get source and destination directories from the user
source_directory = input("Enter the source directory path: ")
destination_directory = input("Enter the destination directory path: ")
# Ask the user how they want to organize the files
organize_by_type = input("Organize by file type? (yes/no): ").lower() == 'yes'
organize_by_date = input("Organize by creation date? (yes/no): ").lower() == 'yes'
organize_by_size = input("Organize by size? (yes/no): ").lower() == 'yes'
organize_files(source_directory, destination_directory, organize_by_type, organize_by_date, organize_by_size)