v2-884e05a1c44172150bf6c2f5feb0d7db_r.jpg

#!/usr/bin/python
# -*- coding:utf-8 -*-

import os,sys
import pyinotify

class OnIOHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        print('Action', "create file: %s " % os.path.join(event.path, event.name))


    def process_IN_DELETE(self, event):
        print('Action', "delete file: %s " % os.path.join(event.path, event.name))


    def process_IN_MODIFY(self, event):
        print('Action', "modify file: %s " % os.path.join(event.path, event.name))


def auto_compile(path='.'):
    wm = pyinotify.WatchManager()
    mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY
    notifier = pyinotify.ThreadedNotifier(wm, OnIOHandler())
    notifier.start()
    wm.add_watch(path, mask, rec=True, auto_add=True)
    print('Start Watch', 'Start monitoring %s' % path)
    while True:
        try:
            notifier.process_events()
            if notifier.check_events():
                notifier.read_events()
        except KeyboardInterrupt:
            notifier.stop()
            break

if __name__ == "__main__":
   auto_compile(tmp)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code