【发布时间】:2018-11-07 13:14:31
【问题描述】:
我正在尝试设置看门狗,以便监控 JavaScript 文件的更改。但是当单个文件被修改时,你总是会得到重复的事件。
我想设置它,这样当文件被修改时,它会查看事件的时间,如果它与前一个事件的秒数相同,那么它不会做任何事情。这样它就可以忽略重复的事件。有没有办法实现这一点,并且总是将上一个事件的时间存储在一个变量中?
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()
【问题讨论】:
标签: python watchdog python-watchdog