5

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.. :)

Bhuvan raj
  • 413
  • 3
  • 8
  • 17

2 Answers2

11
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.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • I have to run a python program So i have to place something like this: `python my_prog.py=['/bin/echo', 'File', ev.pathname, 'changed']`?? – Bhuvan raj May 13 '11 at 20:04
  • @Bhuvan raj No, more like `cmd = ['/usr/bin/env', 'python', './my_prog.py', ev.pathname]` – phihag May 13 '11 at 20:06
  • thanks a lot :) :) :) :) Its working. I would be happy if you could explain what is ev.pathname do in that parameters list. – Bhuvan raj May 13 '11 at 20:22
  • 1
    @Bhuvan raj `ev` is an object that describes the event that caused the file to change. `ev.pathname` is the name of the file the event relates to. In our case, it's a little bit boring - it's always `'file.watched'`. But if we'd register for more than one file, `ev.pathname` would always be the name of the changed file. – phihag May 13 '11 at 20:26
  • @phihag Tried this, but there seems to be a problem. When I'm watching a single file, for each save the loop runs two times. – Musfar Saleem Jun 21 '18 at 07:09
  • 1
    @MusfarSaleem Well, that is perfectly sensible, maybe the file is written in two blocks to? If you want to know of when a file has been saved, for most editors something like a mask of `pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO` would work. If you want to be more precise, you need to define which system calls are run by your editor when you save. – phihag Jun 21 '18 at 11:12
2

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)
macm
  • 1,959
  • 23
  • 25