Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In_close_write event from python watchdog

I'm working on a simple watchdog script that will run md5sum on some very large images uploaded to our FTP. Watchdog doesn't seem to have a IN_CLOSE_WRITE event which exists in pyinotify. I tried checking if the file is still open as a work around but that does not work. Does anyone know a workaround to getting close_write event from watchdog?

import sys
import time

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


path = sys.argv[1]

class MyEventHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print "File uploaded"
        # Is file still uploading?
        f = open(event.src_path)
        if f.closed:
            print "....run md5 & email admin"


event_handler = MyEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()
like image 801
redrodan Avatar asked May 20 '26 18:05

redrodan


1 Answers

Apparently that's not really possible with watchdog. Since watchdog tries to be platform-independent, it only handles events that can be detected on all platforms. There is another question that is relevant: Python (Watchdog) - Waiting for file to be created correctly

Also there is a issue on github, which is closed (basically wontfix): https://github.com/gorakhargosh/watchdog/issues/184

So it seems that going with pyinotify is probably the best option.

like image 115
jeverling Avatar answered May 22 '26 06:05

jeverling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!