简体   繁体   中英

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 understand 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM