python中文件變化監(jiān)控示例(watchdog)
在python中文件監(jiān)控主要有兩個(gè)庫(kù),一個(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主要采用觀察者模型(廢話,從變量命名就可以看出來(lái))。主要有三個(gè)角色:observer,event_handler,被監(jiān)控的文件夾。三者原本是獨(dú)立的,主要通過(guò)observer.schedule函數(shù)將三者串起來(lái),意思為observer不斷檢測(cè)調(diào)用平臺(tái)依賴代碼對(duì)監(jiān)控文件夾進(jìn)行變動(dòng)檢測(cè),當(dāng)發(fā)現(xiàn)改變時(shí),通知event_handler處理。最后特別推薦讀者有時(shí)間可以閱讀一下watchdog的源碼,寫(xiě)的易懂而且架構(gòu)很好。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python如何統(tǒng)計(jì)代碼運(yùn)行的時(shí)長(zhǎng)
這篇文章主要介紹了python如何統(tǒng)計(jì)代碼運(yùn)行的時(shí)長(zhǎng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python+tkinter實(shí)現(xiàn)一個(gè)繪圖風(fēng)格控件
這篇文章主要為大家詳細(xì)介紹了Python如何利用tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的繪圖風(fēng)格控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-09-09
詳解Python如何在多層循環(huán)中使用break/continue
關(guān)于break/continue這兩個(gè)關(guān)鍵字在平常的使用過(guò)程中一直比較迷糊。所以本文將詳細(xì)講講Python如何在多層循環(huán)中使用break/continue,需要的可以參考一下2022-05-05
TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Python集合union()函數(shù)使用實(shí)例詳解
union()方法的工作原理是:返回多個(gè)集合(集合的數(shù)量大于等于2)的并集,即結(jié)果集合包含了所有被合并集合中的所有元素,因?yàn)榧现械脑夭豢芍貜?fù),所以各個(gè)集合中重復(fù)的元素在結(jié)果集合中只會(huì)出現(xiàn)一次,本文將簡(jiǎn)單介紹一下Python union()函數(shù)使用方法2023-07-07
分享2個(gè)方便調(diào)試Python代碼的實(shí)用工具
這篇文章主要介紹了分享方便調(diào)試Python代碼的2個(gè)實(shí)用工具,可以方便展示我們調(diào)試代碼的中間狀態(tài),提升大家的編碼效率,詳細(xì)的介紹需要的小伙伴可以參考一下下面文章內(nèi)容2022-05-05
淺談Python xlwings 讀取Excel文件的正確姿勢(shì)
這篇文章主要介紹了淺談Python xlwings 讀取Excel文件的正確姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

