-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchDog.py
52 lines (39 loc) · 1.39 KB
/
watchDog.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
50
51
52
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class OnMyWatch:
# Set the directory on watch
watchDirectory = "/mnt/muesfs/mues/temp/"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.watchDirectory, recursive = True)
self.observer.start()
try:
while True:
time.sleep(1)
print("watching...")
except:
self.observer.stop()
print("Observer Stopped")
self.observer.join()
#function to return files in a directory
def fileInDirectory(my_dir: str):
onlyfiles = [f for f in listdir(my_dir) if isfile(join(my_dir, f))]
return(onlyfiles)
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
print(event)
if event.is_directory:
return None
elif event.event_type == 'created':
# Event is created, you can process it now
print("Watchdog received created event - % s." % event.src_path)
elif event.event_type == 'modified':
# Event is modified, you can process it now
print("Watchdog received modified event - % s." % event.src_path)
if __name__ == '__main__':
watch = OnMyWatch()
watch.run()