File change detection without polling

I am trying to use a method inside a Python program to determine if a file has been modified in the file system. I know that I could run something every 5 seconds to check the date of the last modification of the system, but I was curious if there is an easier way for this, without having to require my program to re-check.

Does anyone know about such a method?

+60
python
Apr 21 '11 at 1:12
source share
4 answers

There is pyinotify for linux.

On the home page:

Pyinotify is a Python module for monitoring file system changes. Pyinotify relies on the Linux kernel function (integrated in the 2.6.13 kernel) called inotify. inotify is a notifying event; its notifications are exported from the kernel to the user space through three system calls. pyinotify binds these system calls and provides an implementation based on them of a general and abstract way to manipulate these functionality.

Thus, it is obviously not cross-platform and relies on a fairly new version of the kernel. However, as far as I can tell, a kernel requiring support will be true for any mechanism without a poll.

+40
Apr 21 '11 at 1:20
source share

watchdog

An excellent cross platform library for browsing directories.

From the website

Supported Platforms

  • Linux 2.6 (inotify)

  • Mac OS X (FSEvents, kqueue)

  • FreeBSD / BSD (kqueue)

  • Windows (ReadDirectoryChangesW with I / O ports, ReadDirectoryChangesW workflows)

  • Regardless of the OS (polling the disk for directory snapshots and periodically comparing them, slow and not recommended)

I have used it in several projects and it seems to work wonderfully.

+71
Apr 21 '11 at 3:32
source share

The windows have:

watcher which is a good port of python API .NET FileSystemWatcher.

There is also (the one I wrote) dirwatch .

Both rely on the ReadDirectoryChangesW function. Although for real work I would use watcher (proper C extension, good API, support for python 2 and 3).

Mine is basically an experiment that calls the appropriate APIs on windows, so it’s only interesting if you want an example of calling these things from python.

+6
Apr 21 '11 at 2:28
source share

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

+4
Apr 21 '11 at 1:32
source share



All Articles