How can I "watch" a file for modification / change? [duplicate]

As noted, you can use pyinotify:

E.g.:

import webbrowser
import pyinotify

class ModHandler(pyinotify.ProcessEvent):
    # evt has useful properties, including pathname
    def process_IN_CLOSE_WRITE(self, evt):
            webbrowser.open(URL)

handler = ModHandler()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(FILE, pyinotify.IN_CLOSE_WRITE)
notifier.loop()

This is more efficient than polling. The kernel tells you when it does the operation, without you having to constantly ask.


The Linux Kernel has a file monitoring API called inotify. A python binding is pyinotify.

With it, you can build what you want.


Install inotify-tools and write a simple shell script to watch a file.


The other option is to use a checksum. You can use a pattern similar to nose's nosy.py. I use the one from dingus to check my directory for modifications and run the test suite.


Use FAM to put a monitor on the file.