68

I'm trying to use a method within a Python program to detect whether a file on the file system has been modified. I know that I could have something run on an every-5-seconds to check the last modification date off of the system, but I was curious as to whether there's an easier method for doing this, without needing to require my program to check repeatedly.

Does anyone know of such a method?

Thomas Ward
  • 2,714
  • 7
  • 36
  • 51

4 Answers4

83

watchdog

Excellent cross platform library for watching directories.

From the website

Supported Platforms

  • Linux 2.6 (inotify)

  • Mac OS X (FSEvents, kqueue)

  • FreeBSD/BSD (kqueue)

  • Windows (ReadDirectoryChangesW with I/O completion ports; ReadDirectoryChangesW worker threads)

  • OS-independent (polling the disk for directory snapshots and comparing them periodically; slow and not recommended)

I've used it on a couple projects and it seems to work wonderfully.

Facorazza
  • 317
  • 1
  • 15
nemith
  • 927
  • 5
  • 5
  • The command-line tool is great for using in development. Thanks for the tip. – AgDude Dec 22 '11 at 13:39
  • From the example given on the watchdog website, it looks like it requires polling. – Zauberin Stardreamer Sep 15 '13 at 00:35
  • 3
    @ZauberParacelsus: It does not. The example does `time.sleep(1)` to keep the example alive, but watchdog is actually asyncronous and will call event_handler in case of a file change event. – WhyNotHugo Apr 02 '14 at 14:19
  • It seems that *watchdog* only works for directories. Is there a way to watch a non-directory file for changes without poling (using the kernels inotify)? – Brutus Jun 19 '14 at 10:50
46

For linux, there is pyinotify.

From the homepage:

Pyinotify is a Python module for monitoring filesystems changes. Pyinotify relies on a Linux Kernel feature (merged in kernel 2.6.13) called inotify. inotify is an event-driven notifier, its notifications are exported from kernel space to user space through three system calls. pyinotify binds these system calls and provides an implementation on top of them offering a generic and abstract way to manipulate those functionalities.

Thus it is obviously not cross-platform and relies on a new enough kernel version. However, as far as I can see, requiring kernel support would be true about any non-polling mechanism.

Aleksi Torhamo
  • 6,452
  • 2
  • 34
  • 44
7

On windows there is:

watcher, which is a nice python port of the .NET FileSystemWatcher API.

Also there's (the one I wrote) dirwatch.

Both rely on the windows ReadDirectoryChangesW function. Though for real work, I'd use watcher (proper C extension, good API, python 2 & 3 support).

Mine is mostly an experiment calling the relevant APIs on windows, so it's only interesting if you want an example of calling these things from python.

otherchirps
  • 1,836
  • 1
  • 19
  • 26
5

You should also see inotifyx which is very similar to the previously mentioned pyinotify, but is said to have an API which changes less.

AJ00200
  • 16,327
  • 7
  • 23
  • 21