python mkdir覆盖_Python mkdir一个有难度的问题

本文介绍了如何使用Python库pyinotify来监控Linux文件系统的各种事件,如创建、删除、修改等,并提供了具体的代码示例,展示了如何处理这些事件。
摘要由CSDN通过智能技术生成

Inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事件警告,比如删除、读、写和卸载操作等。您还可以跟踪活动的源头和目标等细节。

使用 inotify 很简单:创建一个文件描述符,附加一个或多个监视器(一个监视器 是一个路径和一组事件),然后使用 read 方法从描述符获取事件。read 并不会用光整个周期,它在事件发生之前是被阻塞的。

更好的是,因为 inotify 通过传统的文件描述符工作,您可以利用传统的 select 系统调用来被动地监控监视器和许多其他输入源。两种方法 — 阻塞文件描述符和使用 select— 都避免了繁忙轮询。

python对应的监听文件事件的lib为pyinotify ,由于mac上安装不了inotify,所以直接在网上找的代码,供题主参考;原连接为:http://www.tuicool.com/articles/fQ7bUr;

思路:通过pyinotify监听文件事件,通过轮询文件事件,来做相应的处理;按照生产者消费者的模型处理,生产者轮询文件事件,放进共享队列;消费者轮询消费共享队列中的数据。

inotify is not available on macosx-10.11-intel

import os

import datetime

import pyinotify

import logging

class MyEventHandler(pyinotify.ProcessEvent):

logging.basicConfig(level=logging.INFO,filename='/var/log/monitor.log')

#自定义写入那个文件,可以自己修改

logging.info("Starting monitor...")

def process_IN_ACCESS(self, event):

print "ACCESS event:", event.pathname

logging.info("ACCESS event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def process_IN_ATTRIB(self, event):

print "ATTRIB event:", event.pathname

logging.info("IN_ATTRIB event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def process_IN_CLOSE_NOWRITE(self, event):

print "CLOSE_NOWRITE event:", event.pathname

logging.info("CLOSE_NOWRITE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def process_IN_CLOSE_WRITE(self, event):

print "CLOSE_WRITE event:", event.pathname

logging.info("CLOSE_WRITE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def process_IN_CREATE(self, event):

print "CREATE event:", event.pathname

logging.info("CREATE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def process_IN_DELETE(self, event):

print "DELETE event:", event.pathname

logging.info("DELETE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def process_IN_MODIFY(self, event):

print "MODIFY event:", event.pathname

logging.info("MODIFY event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def process_IN_OPEN(self, event):

print "OPEN event:", event.pathname

logging.info("OPEN event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

def main():

# watch manager

wm = pyinotify.WatchManager()

wm.add_watch('/tmp', pyinotify.ALL_EVENTS, rec=True)

#/tmp是可以自己修改的监控的目录

# event handler

eh = MyEventHandler()

# notifier

notifier = pyinotify.Notifier(wm, eh)

notifier.loop()

if __name__ == '__main__':

main()

[root@centos6 monitor-folder]# python total-monitor.py

OPEN event: /tmp/.ICE-unix

CLOSE_NOWRITE event: /tmp/.ICE-unix

OPEN event: /tmp

CLOSE_NOWRITE event: /tmp

OPEN event: /tmp

CLOSE_NOWRITE event: /tmp

DELETE event: /tmp/aa

DELETE event: /tmp/adduser.conf

DELETE event: /tmp/adjtime

DELETE event: /tmp/aliases

DELETE event: /tmp/bash.bashrc

DELETE event: /tmp/bindresvport.blacklist

DELETE event: /tmp/environment

DELETE event: /tmp/fstab

DELETE event: /tmp/ipt.err

DELETE event: /tmp/ipt.out

DELETE event: /tmp/krb5.conf

DELETE event: /tmp/odbc.ini

DELETE event: /tmp/odbcinst.ini

DELETE event: /tmp/timezone

DELETE event: /tmp/ucf.conf

DELETE event: /tmp/warnquota.conf

DELETE event: /tmp/wgetrc

DELETE event: /tmp/xinetd.conf

CREATE event: /tmp/aa

OPEN event: /tmp/aa

ATTRIB event: /tmp/aa

CLOSE_WRITE event: /tmp/aa

CREATE event: /tmp/bb

OPEN event: /tmp/bb

ATTRIB event: /tmp/bb

CLOSE_WRITE event: /tmp/bb

CREATE event: /tmp/cc

OPEN event: /tmp/cc

ATTRIB event: /tmp/cc

CLOSE_WRITE event: /tmp/cc

上面是打印出来的监控状态,下面是我的操作代码:

[root@centos6 tmp]# ls

aa bash.bashrc ipt.err odbcinst.ini wgetrc

adduser.conf bindresvport.blacklist ipt.out timezone xinetd.conf

adjtime environment krb5.conf ucf.conf

aliases fstab odbc.ini warnquota.conf

[root@centos6 tmp]# rm -rf *

[root@centos6 tmp]# touch aa

[root@centos6 tmp]# touch bb

[root@centos6 tmp]# touch cc

[root@centos6 tmp]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值