Pyinotify是一个Python模块,用来监测文件系统的变化。 Pyinotify依赖于Linux内核的功能—inotify(内核2.6.13合并)。 inotify的是一个事件驱动的通知器,其通知接口通过三个系统调用从内核空间到用户空间。pyinotify结合这些系统调用,并提供一个顶级的抽象和一个通用的方式来处理这些功能。
pyinotify 就是通过 调用系统的inotify来实现通知的
Inotify 既可以监视文件,也可以监视目录
Inotify 使用系统调用而非 SIGIO 来通知文件系统事件。
Inotify 可以监视的文件系统事件包括:
#!/usr/bin/envpython #coding=utf-8 importos frompyinotifyimportWatchManager,Notifier,ProcessEvent,IN_DELETE,IN_CREATE,IN_MODIFY wm=WatchManager() mask=IN_DELETE|IN_CREATE|IN_MODIFY#watchedevents classPFilePath(ProcessEvent): defprocess_IN_CREATE(self,event): print"Createfile:%s"%os.path.join(event.path,event.name) defprocess_IN_DELETE(self,event): print"Deletefile:%s"%os.path.join(event.path,event.name) defprocess_IN_MODIFY(self,event): print"Modifyfile:%s"%os.path.join(event.path,event.name) if__name__=="__main__": notifier=Notifier(wm,PFilePath()) wdd=wm.add_watch('.',mask,rec=True) whileTrue: try: notifier.process_events() ifnotifier.check_events(): notifier.read_events() exceptKeyboardInterrupt: notifier.stop() break
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)