Python中使用Inotify監(jiān)控文件實例
更新時間:2015年02月14日 11:46:05 投稿:junjie
這篇文章主要介紹了Python中使用Inotify監(jiān)控文件實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
Inotify地址:訪問
# -*- coding:utf-8 -*- import os import pyinotify from functions import * WATCH_PATH = '' #監(jiān)控目錄 if not WATCH_PATH: wlog('Error',"The WATCH_PATH setting MUST be set.") sys.exit() else: if os.path.exists(WATCH_PATH): wlog('Watch status','Found watch path: path=%s.' % (WATCH_PATH)) else: wlog('Error','The watch path NOT exists, watching stop now: path=%s.' % (WATCH_PATH)) sys.exit() class OnIOHandler(pyinotify.ProcessEvent): def process_IN_CREATE(self, event): wlog('Action',"create file: %s " % os.path.join(event.path,event.name)) def process_IN_DELETE(self, event): wlog('Action',"delete file: %s " % os.path.join(event.path,event.name)) def process_IN_MODIFY(self, event): wlog('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) wlog('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(WATCH_PATH)
相關(guān)文章
python實現(xiàn)xlwt xlrd 指定條件給excel行添加顏色
這篇文章主要介紹了python實現(xiàn)xlwt xlrd 指定條件給excel行添加顏色,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07python中time模塊指定格式時間字符串轉(zhuǎn)為時間戳
本文主要介紹了python中time模塊指定格式時間字符串轉(zhuǎn)為時間戳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python實現(xiàn)層次分析法及自調(diào)節(jié)層次分析法的示例
這篇文章主要介紹了Python實現(xiàn)層次分析法及自調(diào)節(jié)層次分析法的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04