Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (43 loc) · 1.34 KB
/
Copy pathmain.py
File metadata and controls
51 lines (43 loc) · 1.34 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
import os
import shutil
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
extension_types = {
'images': ['jpg', 'png', 'webp'],
'documents': ['pdf', 'docx', 'xlsx', 'csv', 'txt'],
'archives': ['zip', 'tar', 'rar'],
'executables': ['exe', 'msi', 'jar', 'whl'],
'configuration': ['ini'],
'torrent': ['torrent']
}
def get_category(extension):
for category, ext_list in extension_types.items():
if extension in ext_list:
return category
return 'others'
class Watcher:
DIRECTORY_TO_WATCH = "C:/Users/rahal/Downloads"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Observer Stopped")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_created(event):
if not event.is_directory:
print(f"File {event.src_path} has been created")
# Trigger your Python script or function here
os.system('classify_file.py')
if __name__ == '__main__':
w = Watcher()
w.run()