14

I would like to invoke my chrome or firefox browser when a file that I specify is modified. How could I "watch" that file to do something when it gets modified?

Programmatically it seems the steps are.. basically set a never ending interval every second or so and cache the initial modification date, then compare the date every second, when it changes invoke X.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • Follow this tutorial on using inotify [Inotify Example: Introduction to Inotify with a C Program Example](http://www.thegeekstuff.com/2010/04/inotify-c-program-example/) – thegeek Jul 19 '10 at 08:49
  • This is not a duplicate question, as that question is about windows. – interestedparty333 Sep 16 '20 at 20:31

7 Answers7

31

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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 1
    Thank you, thank you. This is great. Is there a similar lib for Windows (for compatibility reasons)? – Sam Dolan Jul 18 '10 at 05:13
  • 1
    @sdolan, Windows has [Directory Change Notifications](http://msdn.microsoft.com/en-us/library/aa365261%28VS.85%29.aspx). There are cross-platform solutions like pyqt's [QFileSystemWatcher](http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qfilesystemwatcher.html). – Matthew Flaschen Jul 18 '10 at 05:28
  • 1
    @sdolan - also, PyGTK has the GIO file monitor. – detly Jul 18 '10 at 07:28
  • @sdolan just to clarify, mhammond's win32 extensions for windows include the dir change notifications. there is a great example here: http://code.activestate.com/recipes/156178-watching-a-directory-under-win32/ – egbutter Nov 25 '11 at 18:58
  • link is broken. Please fix it. – Wade Anderson Sep 15 '14 at 23:09
9

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

With it, you can build what you want.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
3

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

nwmcsween
  • 361
  • 2
  • 12
2

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.

Sam Dolan
  • 31,966
  • 10
  • 88
  • 84
2

Use FAM to put a monitor on the file.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

use a quick hash function, a cron job, and off you go!

Also, this looks relevant: http://en.wikipedia.org/wiki/Inotify

gtrak
  • 5,598
  • 4
  • 32
  • 41
0

Apparently, watchdog works on both Linux & OSX that can be used to monitor for changes in a directory as well with great example documentation. It also works with python3.x in case you don't want to be forced to use python2.x

james-see
  • 12,210
  • 6
  • 40
  • 47