-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_delete_watcher.py
38 lines (28 loc) · 1018 Bytes
/
file_delete_watcher.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
import sys
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers.polling import PollingObserver
class FileDeletionHandler(FileSystemEventHandler):
def on_deleted(self, event):
if event.is_directory:
print(f"Directory deleted: {event.src_path}")
else:
print(f"File deleted: {event.src_path}. Triggering warning.")
def main(directory_to_watch):
event_handler = FileDeletionHandler()
observer = PollingObserver()
observer.schedule(event_handler, directory_to_watch, recursive=True)
print(f"Starting to watch '{directory_to_watch}' for file deletions.")
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
if len(sys.argv) > 1:
directory_to_watch = sys.argv[1]
main(directory_to_watch)
else:
print("Usage: python file_deletion_watchdog.py <directory_to_watch>")