Login or Sign up

用 inotify 监控文件目录的变化

Posted by: tigerz on Aug. 3, 2009

inotiy:

#/usr/bin/env python
import  os
from  pyinotify import  WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent

wm = WatchManager()
mask = EventsCodes.IN_DELETE | EventsCodes.IN_CREATE    # watched events

class   PTmp(ProcessEvent):
        def   process_IN_CREATE(self, event):
                print   " Create: %s "  %   os.path.join(event.path, event.name)

        def   process_IN_DELETE(self, event):
                print   " Remove: %s "  %   os.path.join(event.path, event.name)

notifier = Notifier(wm, PTmp())
wdd = wm.add_watch('/tmp', mask, rec=True)

while  True:    # loop forever
        try :
                # process the queue of events as explained above
               notifier.process_events()
                if  notifier.check_events():
                        # read notified events and enqeue them
                       notifier.read_events()
                # you can do some tasks here...
        except  KeyboardInterrupt:
                # destroy the inotify's instance on this interrupt (stop monitoring)
               notifier.stop()
                break

Comments on This Post:

Please Login (or Sign Up) to leave a comment