-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_organizer.py
119 lines (93 loc) · 5.12 KB
/
file_organizer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import shutil
def get_category(file_extension):
categories = {
"Documents": ["txt", "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx"],
"Images": ["jpg", "jpeg", "png", "gif", "bmp"],
"Videos": ["mp4", "avi", "mkv", "mov", "wmv"],
"Music": ["mp3", "ogg", "wav", "flac"],
}
for category, extensions in categories.items():
if file_extension.lower() in extensions:
return category
return "Others"
def get_destination_path(filename, destination_folder, sort_by_category):
file_extension = os.path.splitext(filename)[1][1:] # Get the file extension without the dot
if sort_by_category:
category = get_category(file_extension)
destination_path = os.path.join(destination_folder, category)
else:
destination_path = os.path.join(destination_folder, file_extension)
os.makedirs(destination_path, exist_ok=True) # Create the directory if it doesn't exist
return destination_path
def organize_files(source_folder, destination_folder, move_files=True, sort_by_category=True, sort_subfolders=True):
logs = [] # Here we create an empty list to store our logs
total_files_moved = 0
total_errors = 0
total_duplicates = 0
os.makedirs(destination_folder, exist_ok=True) # Create the directory if it doesn't exist
existing_files = set() # To keep track of files already processed
for root, _, files in os.walk(source_folder):
if not sort_subfolders and root != source_folder:
continue
for filename in files:
file_path = os.path.join(root, filename)
if os.path.isfile(file_path):
destination_path = get_destination_path(filename, destination_folder, sort_by_category)
try:
destination_file_path = os.path.join(destination_path, filename)
# Check if the file with the same size already exists in the destination folder
if os.path.exists(destination_file_path):
source_file_size = os.path.getsize(file_path)
dest_file_size = os.path.getsize(destination_file_path)
if source_file_size == dest_file_size and destination_file_path not in existing_files:
total_duplicates += 1
logs.append(f"Skipped (Same Size): '{filename}'")
existing_files.add(destination_file_path)
continue
if move_files:
shutil.move(file_path, destination_file_path)
total_files_moved += 1
logs.append(f"Moved '{filename}' to '{destination_path}'")
else:
shutil.copy2(file_path, destination_file_path)
total_files_moved += 1
logs.append(f"Copied '{filename}' to '{destination_path}'")
except shutil.Error:
total_errors += 1
logs.append(f"Error processing '{filename}'")
summary = f"Total {total_files_moved} files organized with {total_errors} errors. {total_duplicates} duplicates skipped."
# When saving the log, we simply write the contents of our list to the file
save_log = input("Do you want to save the summary and log in separate .log files? (yes/no): ").lower()
if save_log == "yes":
log_file = "organize_files_log.txt"
with open(log_file, "w") as log:
log.write("File Organization Log\n")
log.write("=====================\n")
for log_entry in logs:
log.write(log_entry + "\n")
with open("organize_files_summary.log", "w") as f:
f.write(summary)
shutil.move(log_file, "organize_files_log.log")
print(summary)
if __name__ == "__main__":
print("Example Paths:")
print("Windows Source folder: C:\\Users\\Username\\Documents\\source_folder")
print("Windows Destination folder: D:\\Backup\\destination_folder")
print("Linux Source folder: /home/username/source_folder")
print("Linux Destination folder: /mnt/backup/destination_folder")
print("Note: Please replace the example paths with your desired paths.")
use_current_directory = input("Do you want to use the current directory as the source folder? (yes/no): ").lower()
if use_current_directory == "yes":
source_folder = os.getcwd() # Set the current working directory as the source folder
else:
source_folder = input("Enter the source folder path: ")
destination_folder = input("Enter the destination folder path: ")
copy_files = input("Do you want to copy the files instead of moving them? (yes/no): ").lower()
if copy_files == "yes":
move_files = False
else:
move_files = True
sort_by_category = input("Do you want to sort by file categories? (yes/no): ").lower()
sort_subfolders = input("Do you want to sort files in subfolders? (yes/no): ").lower() == "yes"
organize_files(source_folder, destination_folder, move_files, sort_by_category == "yes", sort_subfolders)