|
2 | 2 | from watchdog.observers import Observer |
3 | 3 | from watchdog.events import FileSystemEventHandler |
4 | 4 | import shutil |
5 | | - |
6 | 5 | class MyHandler(FileSystemEventHandler): |
7 | 6 | def on_created(self, event): |
8 | 7 | if not event.is_directory: |
9 | | - file_path = event.src_path |
10 | | - file_name = os.path.basename(file_path) |
11 | | - file_extension = os.path.splitext(file_name)[1].lower() |
12 | | - |
13 | | - |
14 | | - |
15 | | - # A dictionary takes file extensions & folder to move each formats to. |
16 | | - destination_mapping = { |
17 | | - #Key = Format/extension to handle : Value = Folder to move file format. |
18 | | - |
19 | | - ".zip": r"C:\Users\Precious pc\Documents\Zip files", |
20 | | - ".png": r"C:\Users\Precious pc\Documents\png_files", |
21 | | - ".psd": r"C:\Users\Precious pc\Documents\psd_destination", |
22 | | - ".pdf": r"C:\Users\Precious pc\Documents\pdf_files", |
23 | | - } |
24 | | - |
25 | | - |
26 | | - # Default destination for unknown extensions |
27 | | - other_files = r"C:\Users\Precious pc\Documents\other_files" |
28 | | - |
29 | | - # Get the destination directory for the file extension or use the default |
30 | | - destination = destination_mapping.get(file_extension, other_files) |
31 | | - |
32 | | - # Move the file to the determined destination |
33 | | - |
34 | | - # Check if file already exists in destination & delete it. |
35 | | - if os.path.exists(os.path.join(destination, file_name)): |
36 | | - print(f"File {file_name} already exists in the destination. Deleting it.") |
37 | | - os.remove(os.path.join(destination, file_name)) |
38 | | - |
39 | | - # Move the file to the determined destination |
40 | | - shutil.move(file_path, destination) |
41 | | - |
| 8 | + file_path = event.src_path |
| 9 | + file_name = os.path.basename(file_path) |
| 10 | + file_extension = os.path.splitext(file_name)[1].lower() |
| 11 | + # A dictionary takes file extensions & folder to move each formats to. |
| 12 | + destination_mapping = { |
| 13 | + # Key = Format/extension to handle : Value = Folder to move file format. |
| 14 | + ".zip": r"C:\Users\Precious pc\Documents\Zip files", |
| 15 | + ".png": r"C:\Users\Precious pc\Documents\png_files", |
| 16 | + ".psd": r"C:\Users\Precious pc\Documents\psd_destination", |
| 17 | + ".pdf": r"C:\Users\Precious pc\Documents\pdf_files", |
| 18 | + } |
| 19 | + # Default destination for unknown extensions |
| 20 | + other_files = r"C:\Users\Precious pc\Documents\other_files" |
| 21 | + # Get the destination directory for the file extension or use the default |
| 22 | + destination = destination_mapping.get(file_extension, other_files) |
| 23 | + # Move the file to the determined destination |
| 24 | + # Check if file already exists in destination & delete it. |
| 25 | + if os.path.exists(os.path.join(destination, file_name)): |
| 26 | + print(f"File {file_name} already exists in the destination. Deleting it.") |
| 27 | + os.remove(os.path.join(destination, file_name)) |
| 28 | + # Move the file to the determined destination |
| 29 | + shutil.move(file_path, destination) |
42 | 30 | if __name__ == "__main__": |
43 | 31 | folder_to_watch = r"C:\Users\Precious pc\Documents\monitor" # Replace with the directory you want to monitor |
44 | 32 | event_handler = MyHandler() |
45 | 33 | observer = Observer() |
46 | 34 | observer.schedule(event_handler, path=folder_to_watch, recursive=False) # Set recursive to True if you want to monitor subdirectories |
47 | 35 | observer.start() |
48 | | - |
49 | 36 | try: |
50 | 37 | while True: |
51 | 38 | pass |
52 | 39 | except KeyboardInterrupt: |
53 | 40 | observer.stop() |
54 | 41 | observer.join() |
55 | | - |
56 | | - |
57 | | - |
58 | | - |
59 | | - |
60 | | - |
61 | | - |
62 | | - |
63 | | - |
64 | | - |
65 | | - |
66 | | - |
67 | | - |
68 | | - |
69 | | - |
0 commit comments