Search code examples
pythondbusinotify

python dbus method for watching a directory change?


I have successfully used the python bindings to io_add_watch in my dbus mainloop to react to changes in a known single file. But I have a case now, where I'm running a dbus mainloop, and need to do work when a directory changes.

I've played around with the command line tool inotifywait -m directory, as well as played with some of the examples provided with pyinotify. What's unclear is how I put the two together. Or if I even should. I could just launch a thread that uses a pipe to run inotifywait directly, and then write to a ram file in /run which I had established an io_add_watch to. I'm relatively new to glib/dbus/mainloop, so it's still kind of magic to me. pyinotify seems kind of heavy for my purposes, but I'm working from no experience here.

I'm running on Debian Jessie, using python3. I am not looking for anything cross platform.


Solution

  • PyInotify can easily watch a directory:

    notifier = pyinotify.Notifier(wm, handler)
    wm.add_watch('/tmp', pyinotify.IN_CREATE)
    notifier.loop()
    

    The full tutorial is here: https://github.com/seb-m/pyinotify/wiki/Tutorial#1-using-the-notifier-class-without-timeout