Detect new or changed files using python

I am trying to detect when a new file is created in a directory or when an existing file changes in a directory.

I tried searching for a script that would do this (preferably in python or bash), but turned out to be short. My environment is Linux with Python 2.6

Question related to us

+5
source share
4 answers

You can use giowhich is part of the GLib file system (in python GLib bindings)

import gio

def directory_changed(monitor, file1, file2, evt_type):
   if (evt_type in (gio.FILE_MONITOR_EVENT_CREATED,
       gio.FILE_MONITOR_EVENT_DELETED)):
       print "Changed:", file1, file2, evt_type

gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed)

however, your program must run GLIL mainloop to receive events. One quick way to check this out:

import glib
ml = glib.MainLoop()
ml.run()

GLib - , . , .


, Fedora Core 2. 2? , GIO GLib. Pyinotify, , , .

+13

Linux, pyinotify, - inotify(7) . - .

+4

If you can use PyQt, there is a QFileSystemWatcher that does just that.

+1
source

There is also Python binding for gamin .

0
source

All Articles