|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import datetime |
| 4 | + |
| 5 | +def categorize_by_size(file_size): |
| 6 | + # Define size categories and their ranges in bytes |
| 7 | + size_categories = { |
| 8 | + 'small': (0, 1024), # Up to 1 KB |
| 9 | + 'medium': (1025, 1024 * 1024), # 1 KB to 1 MB |
| 10 | + 'large': (1024 * 1025, float('inf')) # Larger than 1 MB |
| 11 | + } |
| 12 | + |
| 13 | + for category, (min_size, max_size) in size_categories.items(): |
| 14 | + if min_size <= file_size < max_size: |
| 15 | + return category |
| 16 | + |
| 17 | + return 'unknown' |
| 18 | + |
| 19 | +def organize_files(source_dir, destination_dir, organize_by_type=True, organize_by_date=True, organize_by_size=True): |
| 20 | + # Create a dictionary to map file extensions to corresponding folders |
| 21 | + file_types = { |
| 22 | + 'images': ['.png', '.jpg', '.jpeg', '.gif'], |
| 23 | + 'documents': ['.pdf', '.docx', '.txt'], |
| 24 | + 'videos': ['.mp4', '.avi', '.mkv'], |
| 25 | + 'other': [] # Add more categories and file extensions as needed |
| 26 | + } |
| 27 | + |
| 28 | + # Create destination subdirectories if they don't exist |
| 29 | + if organize_by_type: |
| 30 | + for folder in file_types: |
| 31 | + folder_path = os.path.join(destination_dir, folder) |
| 32 | + os.makedirs(folder_path, exist_ok=True) |
| 33 | + |
| 34 | + if organize_by_date: |
| 35 | + for year in range(2010, 2030): # Adjust the range based on your needs |
| 36 | + year_folder = os.path.join(destination_dir, str(year)) |
| 37 | + os.makedirs(year_folder, exist_ok=True) |
| 38 | + |
| 39 | + for month in range(1, 13): |
| 40 | + month_folder = os.path.join(year_folder, f"{month:02d}") |
| 41 | + os.makedirs(month_folder, exist_ok=True) |
| 42 | + |
| 43 | + if organize_by_size: |
| 44 | + for size_category in ['small', 'medium', 'large']: |
| 45 | + size_folder = os.path.join(destination_dir, size_category) |
| 46 | + os.makedirs(size_folder, exist_ok=True) |
| 47 | + |
| 48 | + # Scan the source directory and organize files |
| 49 | + for filename in os.listdir(source_dir): |
| 50 | + file_path = os.path.join(source_dir, filename) |
| 51 | + |
| 52 | + if os.path.isfile(file_path): |
| 53 | + # Determine the file type based on extension |
| 54 | + file_type = None |
| 55 | + for category, extensions in file_types.items(): |
| 56 | + if any(filename.lower().endswith(ext) for ext in extensions): |
| 57 | + file_type = category |
| 58 | + break |
| 59 | + |
| 60 | + if organize_by_type and file_type: |
| 61 | + # Move the file to the corresponding subdirectory |
| 62 | + destination_folder = os.path.join(destination_dir, file_type) |
| 63 | + destination_path = os.path.join(destination_folder, filename) |
| 64 | + shutil.move(file_path, destination_path) |
| 65 | + print(f"Moved: {filename} to {file_type} folder") |
| 66 | + |
| 67 | + if organize_by_date: |
| 68 | + # Get the creation date of the file |
| 69 | + creation_time = os.path.getctime(file_path) |
| 70 | + creation_date = datetime.datetime.fromtimestamp(creation_time) |
| 71 | + |
| 72 | + # Determine the destination folder based on creation date |
| 73 | + destination_folder = os.path.join( |
| 74 | + destination_dir, |
| 75 | + str(creation_date.year), |
| 76 | + f"{creation_date.month:02d}", |
| 77 | + ) |
| 78 | + |
| 79 | + # Move the file to the corresponding subdirectory |
| 80 | + destination_path = os.path.join(destination_folder, filename) |
| 81 | + shutil.move(file_path, destination_path) |
| 82 | + print(f"Moved: {filename} to {creation_date.year}/{creation_date.month:02d} folder") |
| 83 | + |
| 84 | + if organize_by_size: |
| 85 | + # Get the size of the file in bytes |
| 86 | + file_size = os.path.getsize(file_path) |
| 87 | + |
| 88 | + # Determine the destination folder based on file size |
| 89 | + size_category = categorize_by_size(file_size) |
| 90 | + destination_folder = os.path.join(destination_dir, size_category) |
| 91 | + destination_path = os.path.join(destination_folder, filename) |
| 92 | + shutil.move(file_path, destination_path) |
| 93 | + print(f"Moved: {filename} to {size_category} folder") |
| 94 | + |
| 95 | +# Get source and destination directories from the user |
| 96 | +source_directory = input("Enter the source directory path: ") |
| 97 | +destination_directory = input("Enter the destination directory path: ") |
| 98 | + |
| 99 | +# Ask the user how they want to organize the files |
| 100 | +organize_by_type = input("Organize by file type? (yes/no): ").lower() == 'yes' |
| 101 | +organize_by_date = input("Organize by creation date? (yes/no): ").lower() == 'yes' |
| 102 | +organize_by_size = input("Organize by size? (yes/no): ").lower() == 'yes' |
| 103 | + |
| 104 | +organize_files(source_directory, destination_directory, organize_by_type, organize_by_date, organize_by_size) |
0 commit comments