-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOrganiser.py
More file actions
91 lines (77 loc) · 3.13 KB
/
Copy pathFileOrganiser.py
File metadata and controls
91 lines (77 loc) · 3.13 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
import os
import shutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
import errno
# Customize these based on your needs
WATCHED_FOLDER = r"C:\\Users\\rahul\\Downloads" # Change this to your downloads or target folder
# Define sorting rules
EXTENSION_MAPPING = {
'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp','.webp'],
'Documents': ['.pdf', '.docx', '.txt', '.xlsx', '.pptx'],
'Videos': ['.mp4', '.mkv', '.mov'],
'Music': ['.mp3', '.wav'],
'Archives': ['.zip', '.rar', '.7z'],
'Scripts': ['.py', '.js', '.sh', '.bat']
}
NAME_KEYWORDS = {
'OS': ['40. Rahul Bhatnagar Experiment','Auxi','File'],
'SRM RESULT': ['SRM'],
'Games': ['Racing','Game','Games','Rival','Cricket','Tennis'],
'Assignments': ['Assignment', 'assignment']
}
class FileOrganizerHandler(FileSystemEventHandler):
def on_any_event(self, event):
if event.is_directory or not event.event_type in ['created', 'modified']:
return
time.sleep(2) # Wait for download to complete
file_path = event.src_path
file_name = os.path.basename(file_path)
file_ext = os.path.splitext(file_name)[1].lower()
# Skip incomplete files
if file_ext in ['.tmp', '.crdownload', '.part']:
print(f"Skipping temp/incomplete file: {file_name}")
return
# Name-based sorting
for folder, keywords in NAME_KEYWORDS.items():
if any(keyword.lower() in file_name.lower() for keyword in keywords):
self.move_file(file_path, folder)
return
# Extension-based sorting
for folder, extensions in EXTENSION_MAPPING.items():
if file_ext in extensions:
self.move_file(file_path, folder)
return
# Default folder
self.move_file(file_path, "Others")
def move_file(self, src_path, folder_name):
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
target_folder = os.path.join(desktop_path, folder_name)
os.makedirs(target_folder, exist_ok=True)
target_path = os.path.join(target_folder, os.path.basename(src_path))
retries = 3
for i in range(retries):
if os.path.exists(src_path):
try:
shutil.move(src_path, target_path)
print(f"Moved '{src_path}' to '{target_path}'")
return
except OSError as e:
if e.errno == errno.ENOENT:
print(f"Retrying... file not found: {src_path}")
time.sleep(2)
else:
time.sleep(2)
if __name__ == "__main__":
print(f"Watching folder: {WATCHED_FOLDER}")
event_handler = FileOrganizerHandler()
observer = Observer()
observer.schedule(event_handler, WATCHED_FOLDER, recursive=False)
observer.start()
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
observer.stop()
observer.join()