python中檔案變化監控-watchdog

來源:互聯網
上載者:User

標籤:keyword   rup   nts   __init__   不同的   path   .com   AC   ati   

在python中檔案監控主要有兩個庫,一個是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一個是watchdog(http://pythonhosted.org/watchdog/)。pyinotify依賴於Linux平台的inotify,後者則對不同平台的的事件都進行了封裝。因為我主要用於Windows平台,所以下面著重介紹watchdog(推薦大家閱讀一下watchdog實現源碼,有利於深刻的理解其中的原理)。
watchdog在不同的平台使用不同的方法進行檔案檢測。在init.py中發現了如下注釋:

|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 Observerfrom watchdog.events import *import timeclass 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主要採用觀察者模型(廢話,從變數命名就可以看出來)。主要有三個角色:observer,event_handler,被監控的檔案夾。三者原本是獨立的,主要通過observer.schedule函數將三者串起來,意思為observer不斷檢測調用平台依賴代碼對監控檔案夾進行變動檢測,當發現改變時,通知event_handler處理。最後精選讀者有時間可以閱讀一下watchdog的源碼,寫的易懂而且架構很好。

python中檔案變化監控-watchdog

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.