麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > Python > 正文

Python文件監(jiān)聽(tīng)工具pyinotify與watchdog實(shí)例

2020-02-15 23:13:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

pyinotify庫(kù)

支持的監(jiān)控事件

@cvar IN_ACCESS: File was accessed.@type IN_ACCESS: int@cvar IN_MODIFY: File was modified.@type IN_MODIFY: int@cvar IN_ATTRIB: Metadata changed.@type IN_ATTRIB: int@cvar IN_CLOSE_WRITE: Writtable file was closed.@type IN_CLOSE_WRITE: int@cvar IN_CLOSE_NOWRITE: Unwrittable file closed.@type IN_CLOSE_NOWRITE: int@cvar IN_OPEN: File was opened.@type IN_OPEN: int@cvar IN_MOVED_FROM: File was moved from X.@type IN_MOVED_FROM: int@cvar IN_MOVED_TO: File was moved to Y.@type IN_MOVED_TO: int@cvar IN_CREATE: Subfile was created.@type IN_CREATE: int@cvar IN_DELETE: Subfile was deleted.@type IN_DELETE: int@cvar IN_DELETE_SELF: Self (watched item itself) was deleted.@type IN_DELETE_SELF: int@cvar IN_MOVE_SELF: Self (watched item itself) was moved.@type IN_MOVE_SELF: int@cvar IN_UNMOUNT: Backing fs was unmounted.@type IN_UNMOUNT: int@cvar IN_Q_OVERFLOW: Event queued overflowed.@type IN_Q_OVERFLOW: int@cvar IN_IGNORED: File was ignored.@type IN_IGNORED: int@cvar IN_ONLYDIR: only watch the path if it is a directory (new         in kernel 2.6.15).@type IN_ONLYDIR: int@cvar IN_DONT_FOLLOW: don't follow a symlink (new in kernel 2.6.15).           IN_ONLYDIR we can make sure that we don't watch           the target of symlinks.@type IN_DONT_FOLLOW: int@cvar IN_EXCL_UNLINK: Events are not generated for children after they           have been unlinked from the watched directory.           (new in kernel 2.6.36).@type IN_EXCL_UNLINK: int@cvar IN_MASK_ADD: add to the mask of an already existing watch (new          in kernel 2.6.14).@type IN_MASK_ADD: int@cvar IN_ISDIR: Event occurred against dir.@type IN_ISDIR: int@cvar IN_ONESHOT: Only send event once.@type IN_ONESHOT: int@cvar ALL_EVENTS: Alias for considering all of the events.@type ALL_EVENTS: int

python 3.6的demo

import sysimport osimport pyinotifyWATCH_PATH = '/home/lp/ftp' # 監(jiān)控目錄if not WATCH_PATH:  print("The WATCH_PATH setting MUST be set.")  sys.exit()else:  if os.path.exists(WATCH_PATH):    print('Found watch path: path=%s.' % (WATCH_PATH))  else:    print('The watch path NOT exists, watching stop now: path=%s.' % (WATCH_PATH))    sys.exit()# 事件回調(diào)函數(shù)class OnIOHandler(pyinotify.ProcessEvent):  # 重寫文件寫入完成函數(shù)  def process_IN_CLOSE_WRITE(self, event):    # logging.info("create file: %s " % os.path.join(event.path, event.name))    # 處理成小圖片,然后發(fā)送給grpc服務(wù)器或者發(fā)給kafka    file_path = os.path.join(event.path, event.name)    print('文件完成寫入',file_path)  # 重寫文件刪除函數(shù)  def process_IN_DELETE(self, event):    print("文件刪除: %s " % os.path.join(event.path, event.name))  # 重寫文件改變函數(shù)  def process_IN_MODIFY(self, event):    print("文件改變: %s " % os.path.join(event.path, event.name))  # 重寫文件創(chuàng)建函數(shù)  def process_IN_CREATE(self, event):    print("文件創(chuàng)建: %s " % os.path.join(event.path, event.name))def auto_compile(path='.'):  wm = pyinotify.WatchManager()  # mask = pyinotify.EventsCodes.ALL_FLAGS.get('IN_CREATE', 0)  # mask = pyinotify.EventsCodes.FLAG_COLLECTIONS['OP_FLAGS']['IN_CREATE']               # 監(jiān)控內(nèi)容,只監(jiān)聽(tīng)文件被完成寫入  mask = pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE  notifier = pyinotify.ThreadedNotifier(wm, OnIOHandler())  # 回調(diào)函數(shù)  notifier.start()  wm.add_watch(path, mask, rec=True, auto_add=True)  print('Start monitoring %s' % path)  while True:    try:      notifier.process_events()      if notifier.check_events():        notifier.read_events()    except KeyboardInterrupt:      notifier.stop()      breakif __name__ == "__main__":  auto_compile(WATCH_PATH)  print('monitor close')            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲精品久久久久久久久久 | 久久国产精品91 | 日韩黄站 | 性片网站| 国产妇女乱码一区二区三区 | 久久毛片| 亚洲一区成人 | 久久草在线观看视频 | 毛片在线视频观看 | 毛片毛片免费看 | 国产在线地址 | 久久精品视频16 | 久久综合久久美利坚合众国 | 黄色伊人网站 | 久久福利在线 | 欧美 videos粗暴 | 久久99综合久久爱伊人 | 日韩欧美中文字幕视频 | 黄a大片 | 黄色二区三区 | 国产精品视频一区二区三区四 | 欧美国产一区二区三区 | 欧美三区在线 | 黄色网址你懂的 | www.91在线| 日本高清电影在线播放 | 欧美视屏一区二区 | 成人毛片100部 | 久久精品在线免费观看 | 狠狠干网站 | 久久国精品 | 蜜桃视频最新网址 | 国产91九色视频 | 日本中文字幕电影在线观看 | 久久夜夜视频 | 成人在线观看免费观看 | 正在播放91精 | 日本成年免费网站 | 久久久三级免费电影 | 久久精品一区二区三 | 538任你躁在线精品视频网站 |