I'm trying to set up watchdog so I can monitor changes to JavaScript files. But you always get duplicate events when a single file is modified.
I want to set it up so that when a file is modified it looks at the time of the event and if it is the same second as the previous event then it doesn't do anything. This way it can ignore the duplicate events. Is there a way to achieve this and always have the time of the previous event stored in a variable?
import time
from os import path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print(time.ctime(), f'path : {event.src_path}')
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=path.join(path.dirname(__file__), 'static/js'), recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()