python 监控文件目录变化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 监控文件目录变化相关的知识,希望对你有一定的参考价值。
使用os.listdir()监控文件目录:
#!/usr/bin/env python #-*- coding=utf-8 -*- #filename: monitor_dir.py import os import time monitor_dir = "/opt/" now_file = dict([(f,None)for f in os.listdir(monitor_dir)]) while True: new_file = dict([(f,None)for f in os.listdir(monitor_dir)]) added = [f for f in new_file if not f in now_file] removed = [f for f in now_file if not f in new_file] if added: print ("Added:",",".join(added)) if removed: print ("Removed:",",".join(removed)) now_file = new_file
以上是关于python 监控文件目录变化的主要内容,如果未能解决你的问题,请参考以下文章
python watchdog:监控文件系统事件的Python库