4

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()
redrodan
  • 41
  • 1
  • 3

1 Answers1

2

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.

jeverling
  • 2,004
  • 1
  • 20
  • 21
  • I have recently seen a project that uses another approach, by using ctypes to call into libc directly: https://github.com/uktrade/mobius3/blob/master/mobius3.py#L57 Just found that approach interesting, for me pyinotify worked well. – jeverling Mar 29 '20 at 21:18
  • A "couple" years later, but it looks like they did add a `on_closed` for linux now (not documented (yet)), for anyone who happens to stumple across this SO post – Mikael Öhman Aug 01 '23 at 23:04