-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomatic_desktop_windows.py
49 lines (40 loc) · 1.48 KB
/
automatic_desktop_windows.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
import os
import shutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
sourcepath = 'C:/Users/gianni/Downloads/'
destinationpath = {
'.xls': 'C:/Users/gianni/Downloads/xls',
'.xlsx': 'C:/Users/gianni/Downloads/xls',
'.csv': 'C:/Users/gianni/Downloads/csv',
'.txt': 'C:/Users/gianni/Downloads/txt',
'.pdf': 'C:/Users/gianni/Downloads/pdf',
}
def move_it(event):
# print(dir(event))
# print('event:', event)
# print('event_type:', event.event_type)
# print('is_directory:', event.is_directory)
# print('src_path:', event.src_path)
# print('key:', event.key)
# print('----')
if not event.is_directory:
parts = os.path.split(event.src_path)
# print('parts:', parts)
filename = parts[-1]
for ext, dst in destinationpath.items():
if filename.lower().endswith(ext):
shutil.move(event.src_path, os.path.join(dst, filename))
print('move:', filename, '->', dst)
if __name__ == "__main__":
try:
event_handler = FileSystemEventHandler()
event_handler.on_modified = move_it
event_handler.on_created = move_it
# event_handler.on_moved = move_it # ie. rename (but this needs to check `dest_path`)
observer = Observer()
observer.start()
observer.schedule(event_handler, sourcepath, recursive=True)
observer.join()
except KeyboardInterrupt:
print('Stopped by Ctrl+C')