dimanche 3 juillet 2016

Watchdog Observer call process function multiple times


I have a simple code

import time
from watchdog.observers import Observer  
from watchdog.events import PatternMatchingEventHandler 

my_path = "D:\test"
class FileHandler(PatternMatchingEventHandler):
    def process(self, event):
        print event.src_path, event.event_type  # print now only for degug

    def on_modified(self, event):
        self.process(event)

if __name__ == '__main__':
    observer = Observer()
    observer.schedule(MyHandler(), my_path, recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

observer.join()

In my_path there is a lot of subdirectories, which are modified from time to time. Each time the are modified I want my script to execute "process" function. This directory looks like: D:testtest1test2test3test4 and when I am copying some files from other directory, lets say i Copy D:othertest1 (which also have subdirectories) to D:testtest1 then function execute each time every single file is modified. how can I force it to execute only once?

edit: Well, maybe I didnt specified it correctly. Lets say I copy 200files to D:test directory. Then function "process" will execute 200times, while I want it to execute only once.


Aucun commentaire:

Enregistrer un commentaire