python中文件變化監(jiān)控示例(watchdog)
在python中文件監(jiān)控主要有兩個(gè)庫,一個(gè)是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一個(gè)是watchdog(http://pythonhosted.org/watchdog/)。pyinotify依賴于Linux平臺(tái)的inotify,后者則對(duì)不同平臺(tái)的的事件都進(jìn)行了封裝。因?yàn)槲抑饕糜赪indows平臺(tái),所以下面著重介紹watchdog(推薦大家閱讀一下watchdog實(shí)現(xiàn)源碼,有利于深刻的理解其中的原理)。
watchdog在不同的平臺(tái)使用不同的方法進(jìn)行文件檢測(cè)。在init.py中發(fā)現(xiàn)了如下注釋:
|Inotify| Linux 2.6.13+ ``inotify(7)`` based observer |FSEvents| Mac OS X FSEvents based observer |Kqueue| Mac OS X and BSD with kqueue(2) ``kqueue(2)`` based observer |WinApi|(ReadDirectoryChangesW) MS Windows Windows API-based observer |Polling| Any fallback implementation
給出示例代碼如下:
from watchdog.observers import Observer from watchdog.events import * import time class FileEventHandler(FileSystemEventHandler): def __init__(self): FileSystemEventHandler.__init__(self) def on_moved(self, event): if event.is_directory: print("directory moved from {0} to {1}".format(event.src_path,event.dest_path)) else: print("file moved from {0} to {1}".format(event.src_path,event.dest_path)) def on_created(self, event): if event.is_directory: print("directory created:{0}".format(event.src_path)) else: print("file created:{0}".format(event.src_path)) def on_deleted(self, event): if event.is_directory: print("directory deleted:{0}".format(event.src_path)) else: print("file deleted:{0}".format(event.src_path)) def on_modified(self, event): if event.is_directory: print("directory modified:{0}".format(event.src_path)) else: print("file modified:{0}".format(event.src_path)) if __name__ == "__main__": observer = Observer() event_handler = FileEventHandler() observer.schedule(event_handler,"d:/dcm",True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
watchdog主要采用觀察者模型(廢話,從變量命名就可以看出來)。主要有三個(gè)角色:observer,event_handler,被監(jiān)控的文件夾。三者原本是獨(dú)立的,主要通過observer.schedule函數(shù)將三者串起來,意思為observer不斷檢測(cè)調(diào)用平臺(tái)依賴代碼對(duì)監(jiān)控文件夾進(jìn)行變動(dòng)檢測(cè),當(dāng)發(fā)現(xiàn)改變時(shí),通知event_handler處理。最后特別推薦讀者有時(shí)間可以閱讀一下watchdog的源碼,寫的易懂而且架構(gòu)很好。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
通過Python腳本+Jenkins實(shí)現(xiàn)項(xiàng)目重啟
Jenkins是一個(gè)流行的開源自動(dòng)化服務(wù)器,用于快速構(gòu)建、測(cè)試和部署軟件,本文主要介紹了通過Python腳本+Jenkins實(shí)現(xiàn)項(xiàng)目重啟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10在服務(wù)器端實(shí)現(xiàn)無間斷部署Python應(yīng)用的教程
這篇文章主要介紹了在服務(wù)器端實(shí)現(xiàn)無間斷部署Python應(yīng)用的教程,方法主要是Gunicorn進(jìn)行重載,需要的朋友可以參考下2015-04-04python csv實(shí)時(shí)一條一條插入且表頭不重復(fù)問題
這篇文章主要介紹了python csv實(shí)時(shí)一條一條插入且表頭不重復(fù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05python selenium 彈出框處理的實(shí)現(xiàn)
這篇文章主要介紹了python selenium 彈出框處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Python實(shí)現(xiàn)判斷變量是否是函數(shù)方式
這篇文章主要介紹了Python實(shí)現(xiàn)判斷變量是否是函數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02django 通過url實(shí)現(xiàn)簡(jiǎn)單的權(quán)限控制的例子
今天小編就為大家分享一篇django 通過url實(shí)現(xiàn)簡(jiǎn)單的權(quán)限控制的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08