看门狗,用于监视文件修改

时间:2018-10-26 21:57:06

标签: python-3.x python-watchdog

我正在尝试在python中使用Watchdog监视文件修改。但是每次事件被触发两次。我还尝试了修改后的事件,但仍然没有成功,并且触发了两次事件。

    class MyHandler(PatternMatchingEventHandler):
   patterns = ["myfile.TXT"]
   def process(self, event):
       """
       event.event_type
           'created' | 'moved' | 'deleted'
       event.is_directory
           True | False
       event.src_path
           path/to/observed/file
       """
       print(event.src_path, event.event_type)  


   def on_created(self, event):
       self.process(event)
       # self.process(event)
       time.sleep(25)
       historicalSize = -1
       while (historicalSize != os.path.getsize(event.src_path)):
           historicalSize = os.path.getsize(event.src_path)
           print ('historicalSize', historicalSize)
            # time.sleep(10)
       start = time.time()
       print("Copy start")
       time.sleep(5)
       copyfile(src, dst)
       print("file copy has now finished")
       timed = '{:.1f}'.format(time.time() - start)
       print(' time:', timed)

0 个答案:

没有答案