Python watchdog repeating events

I created a modified watchdog example to track the file for .jpg photos that were added to a specific directory in Windows.

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

paths = []

xp_mode = 'off'

class FileHandler(FileSystemEventHandler):

    def on_created(self, event):
        if xp_mode == 'on':
            if not event.is_directory and not 'thumbnail' in event.src_path:
                print "Created: " + event.src_path
                paths.append(event.src_path)

    def on_modified(self, event):
        if not event.is_directory and not 'thumbnail' in event.src_path:
            print "Modified: " + event.src_path
            paths.append(event.src_path)

if __name__ == "__main__":
    path = 'C:\\'
    event_handler = FileHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observe r.stop()

    observer.join()

One of the things I noticed is that when adding a file, both on_created and on_modified are called! To solve this problem, I decided to use only the on_modified method. However, I am starting to notice that this also causes multiple callbacks, but this time the on_modified method!

Modified: C:\images\C121211-0008.jpg
Modified: C:\images\C121211-0009.jpg
Modified: C:\images\C121211-0009.jpg <--- What?
Modified: C:\images\C121211-0010.jpg
Modified: C:\images\C121211-0011.jpg
Modified: C:\images\C121211-0012.jpg
Modified: C:\images\C121211-0013.jpg

I can’t understand why this is happening! This seems to be consistent too. If someone can shed light on this issue, he will be very grateful.

There was a similar post, but it was for Linux: python watchdog changed and created repeating events

0
1

, , .

, , , . , Windows , .

... , , set , list.

+4

Source: https://habr.com/ru/post/1609589/


All Articles