[Python] Linux环境下监控目录变化的Python代码片段 →→→→→进入此内容的聊天室

来自 , 2020-03-30, 写在 Python, 查看 178 次.
URL http://www.code666.cn/view/5516adb1
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3.  
  4. import os
  5. from pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE,IN_MODIFY
  6. wm = WatchManager()
  7. mask = IN_DELETE | IN_CREATE |IN_MODIFY   # watched events
  8.  
  9. class PFilePath(ProcessEvent):
  10.     def process_IN_CREATE(self, event):
  11.         print   "Create file: %s " %   os.path.join(event.path, event.name)
  12.  
  13.     def process_IN_DELETE(self, event):
  14.         print   "Delete file: %s " %   os.path.join(event.path, event.name)
  15.    
  16.     def process_IN_MODIFY(self, event):
  17.             print   "Modify file: %s " %   os.path.join(event.path, event.name)
  18.    
  19.  
  20. if __name__ == "__main__":
  21.    
  22.     notifier = Notifier(wm, PFilePath())
  23.     wdd = wm.add_watch('.', mask, rec=True)
  24.  
  25.     while True:
  26.         try :
  27.             notifier.process_events()
  28.             if notifier.check_events():
  29.                 notifier.read_events()
  30.         except KeyboardInterrupt:
  31.             notifier.stop()
  32.             break
  33. #//python/4307

回复 "Linux环境下监控目录变化的Python代码片段"

这儿你可以回复上面这条便签

captcha