-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganize_files.py
More file actions
64 lines (45 loc) · 1.74 KB
/
organize_files.py
File metadata and controls
64 lines (45 loc) · 1.74 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
import os
import shutil
import sys
CATEGORIES = {
"IMAGES": [".jpeg", ".jpg", ".png", ".gif", ".svg"],
"DOCUMENTS":[".pdf", ".docx", ".txt", ".pptx", ".xlsx", ".csv"],
"AUDIO": [".mp3", ".wav", ".aac"],
"VIDEO": [".mp4", ".mov", ".avi", ".mkv"],
"ARCHIVES": [".zip", ".rar", ".tar", ".gz"],
}
def organize_directory(path):
if not os.path.isdir(path):
print(f"Error: The directory '{path}' was not found.")
return
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
continue
file_extension = os.path.splitext(item)[1].lower()
found_category = None
for category, extensions in CATEGORIES.items():
if file_extension in extensions:
found_category = category
break
if found_category is None:
found_category = "OTHERS"
category_path = os.path.join(path, found_category)
os.makedirs(category_path, exist_ok=True)
destination_path = os.path.join(category_path, item)
shutil.move(item_path, destination_path)
print(f"Moved '{item}' to '{found_category}' folder.")
print("Organization complete!")
def main():
if len(sys.argv) < 2 :
target_directory = input("Please enter the full path of the directory you want to organize: ")
else :
target_directory = sys.argv[1]
organize_directory(target_directory.strip())
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
# To cancel the Operation using Ctrl+C
print("\n\nOperation cancelled by user. Goodbye!")
sys.exit(0)