how to make pyinotify to run a program on any modification over a file?
I have to watch for any input given to or any changes that made in the present content over a file, upon any modification i need to run a python program which is located in the same folder.
I tried my best to und开发者_如何学编程erstand but i'm not able to get any good result. It would be of great help, if anyone can help me through this.
Thank you.. :)
import pyinotify,subprocess
def onChange(ev):
cmd = ['/bin/echo', 'File', ev.pathname, 'changed']
subprocess.Popen(cmd).communicate()
wm = pyinotify.WatchManager()
wm.add_watch('file.watched', pyinotify.IN_MODIFY, onChange)
notifier = pyinotify.Notifier(wm)
notifier.loop()
Replace cmd
with the command you want to execute and file.watched
with the file you want to watch, obviously.
from http://schettino72.wordpress.com/tag/inotify/
I am working on adding some inotify goodness to doit. For that I want to receive one, and one only, notification every time a file is modified. Inotify makes the hard work of watching the file system and Pyinotify provides a python interface. But using it was not straight-forward as I expected. The problem is that editors manipulate files on its own ways…
It worked fine when I used “echo”. But than when I tried with Emacs I got 3 notifications. With VIM it was even worst, I got no notifications and an error message!
Getting the excelent example of phihag
wm.add_watch('file.watched', pyinotify.IN_MODIFY, onChange)
could be:
wm.add_watch('file.watched', pyinotify.IN_CLOSE_WRITE, onChange)
精彩评论