Python的看门狗实现自动化实时对服务器、Windows或Linux文件夹的实时监控
生活随笔
收集整理的這篇文章主要介紹了
Python的看门狗实现自动化实时对服务器、Windows或Linux文件夹的实时监控
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
眾所周知,在運維過程中,實時獲取目標(biāo)文件夾至關(guān)重要,Python的watchdog是用程序來監(jiān)視文件系統(tǒng)事件Python庫,所以用該庫可以實現(xiàn)對文件夾的實時監(jiān)控,filenotify.py代碼如下:
# -*- coding: utf-8 -*- #!/usr/bin/env python # @Time : 2018/2/8 17:48 # @Desc : 監(jiān)控工作目錄文件夾 # @File : filenotify.py # @Software: PyCharmfrom watchdog.observers import Observer from 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()#Windowsobserver.schedule(event_handler, "D:", True)#Linux、服務(wù)器# observer.schedule(event_handler,"/home/../",True) observer.start()try:while True:time.sleep(1)except KeyboardInterrupt:observer.stop()observer.join()監(jiān)控結(jié)果如下:
安裝watchdog命令? pip install watchdog
轉(zhuǎn)載于:https://www.cnblogs.com/IT-LearnHall/p/9426242.html
總結(jié)
以上是生活随笔為你收集整理的Python的看门狗实现自动化实时对服务器、Windows或Linux文件夹的实时监控的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Spring】入门HelloWorld
- 下一篇: JavaScript高级特征之面向对象笔