Python 的 pyinotify 模块 监控文件夹和文件的变动

官方参考:

https://github.com/seb-m/pyinotify/wiki/Events-types

https://github.com/seb-m/pyinotify/wiki/Install

最近在网上看到python有个pyinotify模块,其中他们可以监控文件夹内的文件的创建,修改,读取,删除等系列操作,我修改了下,添加了可以吧操作记录写到日志里的一点方法,下面就贴出代码了给大家分享下:#!/usr/bin/env
python

?





01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

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

   

下面来看看效果如何呢:我在代码定义的是监控tmp目录下的变化:

?





01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

[[email protected] 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

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

[[email protected] 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

[[email protected] tmp]# rm -rf *

[[email protected] tmp]# touch aa

[[email protected] tmp]# touch bb

[[email protected] tmp]# touch cc

[[email protected] tmp]#

上面是直接打印出来的,下面在看看我吧操作记录在日志文件里面,也可以看下日志文件的记录:

?





01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

INFO:root:Starting monitor...

INFO:root:OPEN
event : /tmp/.ICE-unix  2011-10-27
13:31:57.219168

INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix  2011-10-27
13:31:57.219609

INFO:root:OPEN
event : /tmp/ 
2011-10-27
13:32:21.395228

INFO:root:CLOSE_NOWRITE event : /tmp/ 
2011-10-27
13:32:21.398105

INFO:root:OPEN
event : /tmp/ 
2011-10-27
13:32:25.108997

INFO:root:CLOSE_NOWRITE event : /tmp/ 
2011-10-27
13:32:25.110239

INFO:root:CREATE event : /tmp/aa  2011-10-27
13:32:28.004863

INFO:root:OPEN
event : /tmp/aa  2011-10-27
13:32:28.005860

INFO:root:IN_ATTRIB event : /tmp/aa  2011-10-27
13:32:28.006723

INFO:root:CLOSE_WRITE event : /tmp/aa  2011-10-27
13:32:28.014617

INFO:root:CREATE event : /tmp/bb  2011-10-27
13:32:30.149758

INFO:root:OPEN
event : /tmp/bb  2011-10-27
13:32:30.164415

INFO:root:IN_ATTRIB event : /tmp/bb  2011-10-27
13:32:30.164877

INFO:root:CLOSE_WRITE event : /tmp/bb  2011-10-27
13:32:30.165303

INFO:root:CREATE event : /tmp/cc  2011-10-27
13:32:32.725418

INFO:root:OPEN
event : /tmp/cc  2011-10-27
13:32:32.726367

INFO:root:IN_ATTRIB event : /tmp/cc  2011-10-27
13:32:32.727229

INFO:root:CLOSE_WRITE event : /tmp/cc  2011-10-27
13:32:32.735052

INFO:root:CREATE event : /tmp/dd  2011-10-27
13:32:39.771041

INFO:root:OPEN
event : /tmp/dd  2011-10-27
13:32:39.780881

INFO:root:IN_ATTRIB event : /tmp/dd  2011-10-27
13:32:39.781455

INFO:root:CLOSE_WRITE event : /tmp/dd  2011-10-27
13:32:39.781893

INFO:root:Starting monitor...

INFO:root:OPEN
event : /tmp/.ICE-unix  2011-10-27
14:01:43.742477

INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix  2011-10-27
14:01:43.742915

INFO:root:OPEN
event : /tmp/ 
2011-10-27
14:01:50.579778

INFO:root:CLOSE_NOWRITE event : /tmp/ 
2011-10-27
14:01:50.581317

INFO:root:DELETE event : /tmp/aa  2011-10-27
14:01:54.999528

INFO:root:DELETE event : /tmp/bb  2011-10-27
14:01:58.995966

INFO:root:DELETE event : /tmp/cc  2011-10-27
14:02:02.795950

INFO:root:DELETE event : /tmp/dd  2011-10-27
14:02:06.284208

INFO:root:OPEN
event : /tmp/ 
2011-10-27
14:02:07.738560

INFO:root:CLOSE_NOWRITE event : /tmp/ 
2011-10-27
14:02:07.741922

INFO:root:CREATE event : /tmp/aa  2011-10-27
14:02:11.110322

INFO:root:OPEN
event : /tmp/aa  2011-10-27
14:02:11.113150

INFO:root:IN_ATTRIB event : /tmp/aa  2011-10-27
14:02:11.116381

INFO:root:CLOSE_WRITE event : /tmp/aa  2011-10-27
14:02:11.118382

INFO:root:Starting monitor...

INFO:root:OPEN
event : /tmp/.ICE-unix  2011-10-27
21:39:12.520432

INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix  2011-10-27
21:39:12.520879

INFO:root:OPEN
event : /tmp/ 
2011-10-27
21:39:23.784759

INFO:root:CLOSE_NOWRITE event : /tmp/ 
2011-10-27
21:39:23.793211

INFO:root:OPEN
event : /tmp/ 
2011-10-27
21:39:33.916232

INFO:root:CLOSE_NOWRITE event : /tmp/ 
2011-10-27
21:39:33.916823

INFO:root:DELETE event : /tmp/aa  2011-10-27
21:39:33.939008

INFO:root:DELETE event : /tmp/adduser.conf  2011-10-27
21:39:33.958143

INFO:root:DELETE event : /tmp/adjtime  2011-10-27
21:39:33.962497

INFO:root:DELETE event : /tmp/aliases  2011-10-27
21:39:33.978506

INFO:root:DELETE event : /tmp/bash.bashrc  2011-10-27
21:39:33.980834

INFO:root:DELETE event : /tmp/bindresvport.blacklist  2011-10-27
21:39:33.997176

INFO:root:DELETE event : /tmp/environment  2011-10-27
21:39:33.997683

INFO:root:DELETE event : /tmp/fstab  2011-10-27
21:39:33.998110

INFO:root:DELETE event : /tmp/ipt.err  2011-10-27
21:39:33.998532

INFO:root:DELETE event : /tmp/ipt.out  2011-10-27
21:39:34.000360

INFO:root:DELETE event : /tmp/krb5.conf  2011-10-27
21:39:34.000816

INFO:root:DELETE event : /tmp/odbc.ini  2011-10-27
21:39:34.002217

INFO:root:DELETE event : /tmp/odbcinst.ini  2011-10-27
21:39:34.002675

INFO:root:DELETE event : /tmp/timezone  2011-10-27
21:39:34.003110

INFO:root:DELETE event : /tmp/ucf.conf  2011-10-27
21:39:34.003538

INFO:root:DELETE event : /tmp/warnquota.conf  2011-10-27
21:39:34.018152

INFO:root:DELETE event : /tmp/wgetrc  2011-10-27
21:39:34.018641

INFO:root:DELETE event : /tmp/xinetd.conf  2011-10-27
21:39:34.041880

INFO:root:CREATE event : /tmp/aa  2011-10-27
21:39:40.639965

INFO:root:OPEN
event : /tmp/aa  2011-10-27
21:39:40.640914

INFO:root:IN_ATTRIB event : /tmp/aa  2011-10-27
21:39:40.647835

INFO:root:CLOSE_WRITE event : /tmp/aa  2011-10-27
21:39:40.652158

INFO:root:CREATE event : /tmp/bb  2011-10-27
21:39:43.064526

INFO:root:OPEN
event : /tmp/bb  2011-10-27
21:39:43.070849

INFO:root:IN_ATTRIB event : /tmp/bb  2011-10-27
21:39:43.071329

INFO:root:CLOSE_WRITE event : /tmp/bb  2011-10-27
21:39:43.071762

INFO:root:CREATE event : /tmp/cc  2011-10-27
21:39:47.046752

INFO:root:OPEN
event : /tmp/cc  2011-10-27
21:39:47.051537

INFO:root:IN_ATTRIB event : /tmp/cc  2011-10-27
21:39:47.056211

INFO:root:CLOSE_WRITE event : /tmp/cc  2011-10-27
21:39:47.057490

[[email protected] tmp]#<br><br><br><a href="http://www.pyshell.com/index.php/archives/477">http://www.pyshell.com/index.php/archives/477</a>

时间: 2024-10-13 16:18:04

Python 的 pyinotify 模块 监控文件夹和文件的变动的相关文章

python文件夹遍历,文件操作,获取文件修改创建时间

在Python中,文件操作主要来自os模块,主要方法如下: os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回当前目录('.')os.chdir(dirname):改变工作目录到dirname os.path.isdir(name):判断name是不是一个目录,name不是目录就返回falseos.path.isfile(name):判断name是不是一个文件,不存在name也返回falseos.path.ex

Python学习(九)IO 编程 —— 文件夹及文件操作

Python 文件夹及文件操作 我们经常会与文件和目录打交道,对于这些操作,python可以使用 os 及 shutill 模块,其中包含了很多操作文件和目录的函数.这边,仅介绍通常会用到的方法. os 可以操作简单的文件夹及文件操作,引入用  import os,可用  help(os)  或是  dir(os)  查看其用法.注意有些函数在os模块中,有的是在os.path模块中. shutil 模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.须

Python 文件夹及文件操作

我们经常会与文件和目录打交道,对于这些操作,python可以使用 os 及 shutill 模块,其中包含了很多操作文件和目录的函数. os 可以执行简单的文件夹及文件操作,引入用  import os,可用  help(os)  或是  dir(os)  查看其用法.注意有些函数在os模块中,有的是在os.path模块中. shutil 模块提供了大量的文件的高级操作,特别针对文件拷贝和删除.主要功能为目录和文件操作以及压缩操作.须引入 import shutil  ,具体 help.本文仅介

python 实现彻底删除文件夹和文件夹下的文件

python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直到删除.但是,我们可以结合多个内置库函数,达到一次删除非空文件夹,不管其目录层次有多深. import os import shutil import traceback import globalvar def misc_init() # clean the test result folder

Python文件夹与文件的操作 ZZ

最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模块, 导入的方法是: import os 一.取得当前目录 s = os.getcwd() # s 中保存的是当前目录(即文件夹) 比如运行abc.py,那么输入该命令就会返回abc所在的文件夹位置. 举个简单例子,我们将abc.py放入A文件夹.并且希望不管将A文件夹放在硬盘的哪个位置,都可以在A

有关文件夹与文件的查找,删除等功能 在 os 模块中实现

最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模块, 导入的方法是: import os 一.取得当前目录 s = os.getcwd() # s 中保存的是当前目录(即文件夹) 比如运行abc.py,那么输入该命令就会返回abc所在的文件夹位置. 举个简单例子,我们将abc.py放入A文件夹.并且希望不管将A文件夹放在硬盘的哪个位置,都可以在A

Python - 创建目录,切换目录,创建文件,复制文件,移动文件 ,删除文件夹及文件

# -*- coding: utf-8 -*- """ Created on Mon Jan  9 00:35:43 2017 @author-Weixin: 953558077 """ import os import shutil import time ''' shutil模块的基础功能简介 copyfile( src, dst)      从源src复制到dst中去.当然前提是目标地址是具备可写权限.抛出的异常信息为IOException

Python文件夹与文件的操作

最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模块, 导入的方法是: import os 一.取得当前目录 s = os.getcwd() # s 中保存的是当前目录(即文件夹) 比如运行abc.py,那么输入该命令就会返回abc所在的文件夹位置. 举个简单例子,我们将abc.py放入A文件夹.并且希望不管将A文件夹放在硬盘的哪个位置,都可以在A

C# FileSystemWatcher 在监控文件夹和文件时的用法

概述 最近学习FileSystemWatcher的用法,它主要是监控一个文件夹,当文件夹内的文件要是有更改就要记录下来,我就整理下我对FileSystemWatcher 的理解和用法. FileSystemWatcher 用法 在应用FileSystemWatcher对象之前,你必须了解这个对象的一些基本属性和事件.毫无疑问,这个对象的最重要的属性为“EnableRaisingEvents”属性. 这个属性决定对象在收到改变通知时是否提交事件.如果EnableRaisingEvents属性设为假