在python中 文件监控主要有两个库,

一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),pyinotify依赖于Linux平台的inotify。

一个是watchdog(http://pythonhosted.org/watchdog/)。对不同平台的的事件都进行了封装。

1、watchdog实现

  

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()

2、pyinotify  

  http://outofmemory.cn/code-snippet/84447/Linux-environment-jiankong-directory-variety-Python-code-segment

http://www.cppcns.com/jiaoben/python/125108.html

Linux 目录变化监听 - python代码实现的更多相关文章

  1. linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现

    1 TCP简介 tcp是一种基于流的应用层协议,其“可靠的数据传输”实现的原理就是,“拥塞控制”的滑动窗口机制,该机制包含的算法主要有“慢启动”,“拥塞避免”,“快速重传”. 2 TCP socket ...

  2. linux实时文件事件监听--inotify

    一.inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系 ...

  3. Vue中使用computed与watch结合实现数据变化监听

    目的:当数据变化时,为其中重要数据增加边框,实现闪烁以达到提醒目的.数据格式如下,只有在未处理火警/故障时增加闪烁边框.可以使用watch进行深度监听.数据格式已定,也非常明确要监听的数据是有两个.既 ...

  4. linux 开启oracle监听

    secureCRT连接到数据库所在的linux机器,切换到oracle用户模式下 [root@nstlbeta ~]# su - oracle 步骤阅读 2 然后用sqlplus登录到数据库,关闭数据 ...

  5. LINUX启动ORACLE监听和服务

    可通过secureCRT或者telnet直接连 启动监听命令:lsnrctl start 成功启动后:sqlplus /nolog 回车 conn / as sysdba 回车 startup 回车 ...

  6. Android软键盘的隐藏显示、事件监听的代码

    把开发过程中重要的一些内容片段做个珍藏,如下资料是关于Android软键盘的隐藏显示.事件监听的内容,应该是对小伙伴们有所用途. public class ResizeLayout extends L ...

  7. angular5 路由变化监听

    1.路由监听 //监听路由变化this.router.events .filter(event => event instanceof NavigationEnd) .map(() => ...

  8. FocusChange-焦点变化监听事件

    想要监听一个控件的焦点变化情况,发现了一个 view.setOnFocusChangeListener(new OnFocusChangeListener() { ...... } 现在写一个小dem ...

  9. 了解linux web的监听工具

    zabbix cacti Nagios 本想安装的,但是安装需要一个 空的服务器,因为服务器已经有安装 LAMP,故没有去了解 尝试了 cacti ,因为本地环境版本问题,只能使用0.8.8a版本,并 ...

随机推荐

  1. Activity的生命周期及协同作用

    生命周期 onCreate:创建Activity的实例对象的方法 onStart:启动当前的activity实例的方法 onResume:如果该方法执行,页面的实例和用户即可以交互 onPause:如 ...

  2. linux服务器常用操作和命令

    1. 什么是linux服务器load average? Load是用来度量服务器工作量的大小,即计算机cpu任务执行队列的长度,值越大,表明包括正在运行和待运行的进程数越多.参考资料:http://e ...

  3. WAFの基本防护透明流模式v1.0

                      一.WAFの透明流模式     1)首先先配置WAF的网络,配置一个网桥接口,设置IP便于带内管理.             2)当然,如果需要不同网段之间都能够管 ...

  4. Java 的 String.split 函数,消除空字符串

    代码: String str = "the music made it hard to concentrate"; String delims = "[ ]+" ...

  5. NO6 alias-unalias命令,递归创建目录,如何取消覆盖提示

    ·如果需要更新补丁则执行:·yum update·yum install lrzsz telnet tree nmap nc -y·alias #查看系统现有的别名.        一.设置别名eg: ...

  6. knockoutjs 经验总结

    http://knockoutjs.com/documentation/introduction.html knockout的模式 MVVM 四大重要概念 声明式绑定UI界面自动刷新依赖跟踪模版 一些 ...

  7. Arduino nano的bootloader文件烧录

    1.买了了nano还没用就用 avrisp烧录器给烧了其他程序,仅仅是的avr单片机了:2.将他恢复成Arduino nano吧. 在Arduino软件安装目录中的hardware\arduino中. ...

  8. GLConsole的编译和使用

    1.在CVars文件中搜索tinyxml,去掉所有tinyxml的编译选项 2.Add the following line to the "CMakeLists.txt" fil ...

  9. POJ 2104 求序列里第K大 主席树裸题

    给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...

  10. postman测试带有json数据格式的字段

    测试六个字段 普通字段: ModelCode 普通字段: MmodelCode 普通字段: ModelTagKey 普通字段: ModelTagValue 普通字段: ModelTagType jso ...