-
Notifications
You must be signed in to change notification settings - Fork 0
/
folderwatcher.py
49 lines (38 loc) · 1.47 KB
/
folderwatcher.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
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, FileModifiedEvent
from pathandler import move_to_folder
path_monitoring = "D:/Users/user/Desktop/a"
path_destination = 'D:/Users/user/Desktop/b'
temporary_formats = [
'.tmp', '.crdownload',
'.partial', '.opdownload',
]
img_formats = [
'.jpg', '.jpeg', '.png', '.svg',
'.gif', '.tiff', '.bmp', '.raw',
'.psd',
]
class MyFileHandler(FileSystemEventHandler):
def on_modified(self, event: FileModifiedEvent):
fileformat = Path(event.src_path).suffix
if isinstance(event, FileModifiedEvent):
try:
if not fileformat in temporary_formats:
if fileformat in img_formats:
move_to_folder(event.src_path, path_destination, folder_name="Images")
else:
move_to_folder(event.src_path, path_destination, folder_name=fileformat.upper())
except FileNotFoundError:
#I have found that on_modified events can 'fire' twice for the same file.
#I solved this with just pass the exception
pass
else:
print(f"UNEXPECTED EVENT: {event.src_path}=={event.event_type}!")
event_handler = MyFileHandler()
observer = Observer()
observer.schedule(event_handler, path_monitoring)
observer.start()
print()
print(f'WATCHING:\n-->{path_monitoring}\n...')
observer.join()