python watchdog modified and created duplicate eve

2019-01-26 16:25发布

问题:

Running on Ubuntu, every time i create a file i get a modified and a created event.

Is this by design or am i doing something wrong?

I'm using the event handler class PatternMatchingEventHandler

event_handler = MediaFileHandler(ignore_directories=True) 
observer = Observer() 
observer.schedule(event_handler, path=directory, recursive=True) 
observer.start()

If this is the correct behavior, can i safely ignore the created event?

回答1:

Short answer: f = open(... , 'w') generates a FileCreatedEvent, f.flush() or f.close() can generate a FileModifiedEvent. So yes, creating a file often generates both FileCreatedEvent and FileModifiedEvents.

Whether or not you can safely ignore FileCreatedEvents depends on what you are trying to do. If you are interested in reacting whenever a file is created, then you need to handle FileCreatedEvents, and perhaps ignore FileModifiedEvents, since it is possible to generate FileModifiedEvents when modifying a file without generating FileCreatedEvents.

Play around with the canonical watchdog script (below) and all should be clearer.


Long answer: To see what is happening, run the canonical watchdog program straight from the docs:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

And from a terminal:

% mkdir ~/tmp
% cd ~/tmp
% script.py 

Now in a Python interpreter when you open a file in w mode:

In [126]: f = open('/home/unutbu/tmp/foobar', 'w')

The terminal prints

2014-02-05 16:29:34 - <FileCreatedEvent: src_path=/home/unutbu/tmp/foobar>

When you write to the file watchdog does not report any event:

In [127]: f.write('Hi')

But when you flush,

In [128]: f.flush()

it reports a FileModifiedEvent:

2014-02-05 16:29:55 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>

If you write more stuff to the file:

In [129]: f.write(' there')

similarly, a FileModifiedEvent is reported when you close the file, since more output is flushed to disk:

In [130]: f.close()

2014-02-05 16:30:12 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>