Python Watchdog 实行对文件或目录的监控
2017-05-06 22:09
1046 查看
watchdog 库支持多平台,受到越来越多的关注。顾名思义,watchdog库就是对文件或者目录(event)来监控以及作出反应。我们对文件作出删除,生成,修改和移动时,event就代表你所做的行为传入到FileSystemEvent类进行处理,然后通过FileSystemEventHandler来作出反应。
FileSystemEvent(event)—>FileSystemEventHandler(event)
即:
watchdog.event.Dir/FileDeletedEvent(event)**—>**self.on_deleted(event)
watchdog.event.Dir/FileCreatedEvent(event)**—>**self.on_created(event)
watchdog.event.Dir/FileModifiedEvent(event)**—>**self.on_modified(event)
watchdog.event.Dir/FileMovedEvent(event)**—>**self.on_moved(event)
以上这些监控和改变均是通过watchdog.observers.Observer()来实现的。
以下为官方实例:
FileSystemEvent(event)—>FileSystemEventHandler(event)
即:
watchdog.event.Dir/FileDeletedEvent(event)**—>**self.on_deleted(event)
watchdog.event.Dir/FileCreatedEvent(event)**—>**self.on_created(event)
watchdog.event.Dir/FileModifiedEvent(event)**—>**self.on_modified(event)
watchdog.event.Dir/FileMovedEvent(event)**—>**self.on_moved(event)
以上这些监控和改变均是通过watchdog.observers.Observer()来实现的。
以下为官方实例:
import sys import time import logging from watchdog.observers import Observer from watchdog.events import LoggingEventHandler if __name__ == "__main__": logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') path = sys.argv[1] if len(sys.argv) > 1 else '.' event_handler = LoggingEventHandler() observer = Observer() observer.schedule(event_handler, path, recursive=True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
相关文章推荐
- python监控文件或目录变化
- Python监控文件变化:watchdog
- Python监控目录和文件变化
- python利用watchdog监控目录并上传到ftp
- python 监控FTP目录下的文件个数
- python中文件变化监控-watchdog
- Python监控文件变化:watchdog
- Python for Windows——监控Windows某个目录下文件的变化
- python watchdog:监控文件系统事件的Python库
- python监控文件或目录变化
- python watchdog:监控文件系统事件的Python库
- python中文件变化监控示例(watchdog)
- python中文件和目录操作
- Python 3.x (2) :文件,目录和路径
- Python 3 教程二:文件,目录和路径
- Python 3 教程二:文件,目录和路径
- python 获取文件列表(或是目录例表)
- Python 3 教程二:文件,目录和路径
- python获得当前目录下文件,并写到name.txt
- python 实现生成目录树及显示文件大小