Reputation: 41
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()
Upvotes: 4
Views: 2879
Reputation: 2084
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.
Upvotes: 2