pyinotify 的简单应用

import os
import datetime
import pyinotify
import logging

set_ = set()
class MyEventHandler(pyinotify.ProcessEvent):

    def process_IN_CLOSE_WRITE(self, event):
        if "/tmp/pycharm_project_741" != event.pathname and not event.pathname.split('/tmp/pycharm_project_741/')[1].startswith('.'):
            set_.add(event.pathname)
        print("close:  ",set_)

    def process_IN_CREATE(self, event):
        print("startswith: ", event.pathname.split('/tmp/pycharm_project_741/')[1])
        # if "/tmp/pycharm_project_741" != event.pathname and not event.pathname.split('/tmp/pycharm_project_741/')[1].startswith('.'):
        #     set_.add(event.pathname)
        # print("CREATE event:", event.pathname)

    # def process_IN_MODIFY(self, event):
    #     #被修改
    #     print("MODIFY event:", event.pathname)

    def process_IN_OPEN(self, event):
        pass
        # if "/tmp/pycharm_project_741" != event.pathname and not event.pathname.split('/tmp/pycharm_project_741/')[1].startswith('.'):
        #     set_.add(event.pathname)
        # print("OPEN event:", event.pathname)


def main():
    # watch manager
    wm = pyinotify.WatchManager()
    wm.add_watch('/tmp/pycharm_project_741/', pyinotify.ALL_EVENTS, rec=True)
    # /tmp是可以自己修改的监控的目录
    # event handler
    eh = MyEventHandler()

    # notifier
    notifier = pyinotify.Notifier(wm, eh)
    notifier.loop()


if __name__ == '__main__':
    main()

posted @ 2021-01-26 13:00  Tank-Li  阅读(145)  评论(0编辑  收藏  举报