-
Notifications
You must be signed in to change notification settings - Fork 726
/
main.py
104 lines (85 loc) · 4.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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)