-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganizer.py
More file actions
175 lines (122 loc) · 3.55 KB
/
Copy pathorganizer.py
File metadata and controls
175 lines (122 loc) · 3.55 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import shutil
from datetime import datetime
# File categories
FILE_TYPES = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
"Documents": [".pdf", ".doc", ".docx", ".txt"],
"Spreadsheets": [".xls", ".xlsx", ".csv"],
"Videos": [".mp4", ".avi", ".mov", ".mkv"],
"Audio": [".mp3", ".wav", ".aac"],
"Archives": [".zip", ".rar", ".7z"]
}
def get_unique_filename(destination_folder, filename):
"""
Prevent overwriting files with same name.
"""
base, extension = os.path.splitext(filename)
counter = 1
new_filename = filename
while os.path.exists(
os.path.join(destination_folder, new_filename)
):
new_filename = f"{base}_{counter}{extension}"
counter += 1
return new_filename
def organize_folder(folder_path):
stats = {}
log_entries = []
for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
if os.path.isdir(item_path):
continue
moved = False
for category, extensions in FILE_TYPES.items():
if item.lower().endswith(tuple(extensions)):
category_folder = os.path.join(
folder_path,
category
)
os.makedirs(
category_folder,
exist_ok=True
)
safe_name = get_unique_filename(
category_folder,
item
)
shutil.move(
item_path,
os.path.join(category_folder, safe_name)
)
stats[category] = stats.get(category, 0) + 1
log_entries.append(
f"{item} -> {category}"
)
moved = True
break
if not moved:
other_folder = os.path.join(
folder_path,
"Others"
)
os.makedirs(
other_folder,
exist_ok=True
)
safe_name = get_unique_filename(
other_folder,
item
)
shutil.move(
item_path,
os.path.join(other_folder, safe_name)
)
stats["Others"] = stats.get(
"Others",
0
) + 1
log_entries.append(
f"{item} -> Others"
)
create_log(folder_path, stats, log_entries)
def create_log(folder_path, stats, log_entries):
log_file = os.path.join(
folder_path,
"organizer_log.txt"
)
with open(log_file, "w") as file:
file.write(
"SMART FILE ORGANIZER REPORT\n"
)
file.write(
"=" * 35 + "\n\n"
)
file.write(
f"Generated: {datetime.now()}\n\n"
)
file.write("Statistics:\n")
total = 0
for category, count in stats.items():
file.write(
f"{category}: {count}\n"
)
total += count
file.write(
f"\nTotal Files: {total}\n\n"
)
file.write(
"Files Moved:\n"
)
file.write(
"-" * 35 + "\n"
)
for entry in log_entries:
file.write(entry + "\n")
print("\nOrganization Complete!")
print(f"Total files moved: {total}")
print(f"Log saved to: {log_file}")
folder = input(
"Enter folder path to organize: "
)
organize_folder(folder)