pyinotify學習總結

2021-08-31 01:44:54 字數 3697 閱讀 2721

pyinotify是乙個python模組,用來監測檔案系統的變化。 pyinotify依賴於linux核心的功能—inotify(核心2.6.13合併)。

1、安裝:

#pip install pyinotify

或者 git clone

cd pyinotify/

python setup.py install

class processevent(_processevent):

__init__(self, pevent=none, **kargs):

my_init(self, **kargs):

nested_pevent(self)

process_in_q_overflow(self, event):

process_default(self, event)

process_in_ignored(self, raw_event):

process_in_move_self(self, raw_event):

process_in_moved_to(self, raw_event):

process_in_moved_from(self, raw_event):

process_in_create(self, raw_event):

cleanup(self)

class notifier:

__init__(self, watch_manager, default_proc_fun=none, read_freq=0, threshold=0, timeout=none):

proc_fun(self):

coalesce_events(self, coalesce=true):

check_events(self, timeout=none):

read_events(self)

process_events(self):

fork_daemon():

loop(self, callback=none, daemonize=false, **args):

stop(self):

class threadednotifier(threading.thread, notifier):

__init__(self, watch_manager, default_proc_fun=none, read_freq=0,threshold=0, timeout=none):

stop(self):

loop(self)

run(self):

class tornadoasyncnotifier(notifier):

__init__(self, watch_manager, ioloop, callback=none, default_proc_fun=none, read_freq=0, threshold=0, timeout=none,channel_map=none):

stop(self):

handle_read(self, *args, **kwargs):

class asyncionotifier(notifier): asyncio/trollius event loop adapter.

__init__(self, watch_manager, loop, callback=none,default_proc_fun=none, read_freq=0, threshold=0, timeout=none):

stop(self):

handle_read(self, *args, **kwargs):

class excludefilter:

__init__(self, arg_lst):

examples:

ef1 = excludefilter(["/etc/rc.*", "/etc/hostname"])

ef2 = excludefilter("/my/path/exclude.lst")

class watchmanager:

__init__(self, exclude_filter=lambda path: false)

close(self)

get_fd(self)

get_watch(self, wd)

del_watch(self, wd):

add_watch(self, path, mask, proc_fun=none, rec=false,auto_add=false, do_glob=false, quiet=true, exclude_filter=none)

update_watch(self, wd, mask=none, proc_fun=none, rec=false, auto_add=false, quiet=true):

get_wd(self, path):

get_path(self, wd):

rm_watch(self, wd, rec=false, quiet=true):

watch_transient_file(self, filename, mask, proc_class):監控乙個臨時檔案,它可以被頻繁多次建立、刪除。

get_ignore_events(self):得到忽視事件

set_ignore_events(self, nval):設定忽視的事件

class rawoutputformat://格式化字串

__init__(self, format=none):

******(self, s, attribute):

punctuation(self, s):

field_value(self, s):

field_name(self, s):

class_name(self, s):

class coloredoutputformat(rawoutputformat):

__init__(self):

compatibility_mode():

command_line():

3、使用例項:

#!/usr/bin/python3

import pyinotify

import time

import os

class processtransientfile(pyinotify.processevent):

def process_in_modify(self,event):

line = f.readline()

if line:

print(line)

filename = './test.text'

f = open(filename,'r')

st_results = os.stat(filename)

st_size = st_results[6]

f.seek(st_size)

print(st_size)

wm = pyinotify.watchmanager()

notifier = pyinotify.notifier(wm)

wm.watch_transient_file(filename,pyinotify.in_modify, processtransientfil

e)notifier.loop()

pyinotify 監控檔案變化

import pyinotify test.txt 是要監控的檔案 f name test.txt 初始化,指向檔案末尾 pos f.seek 0,2 def read line global pos global f try f.seek pos,0 line f.readline pos len...

使用pyinotify監控檔案系統變化

import pyinotify wm pyinotify.watchmanager 建立乙個wachmanager物件 mask pyinotify.in create pyinotify.in delete 建立要監控的事件,這裡是監控建立檔案與刪除檔案事件 wm.add watch 要監控的檔...

學習後總結,總結後再學習

學習後總結,總結後再學習 2010年畢業以來,讀了幾百本書籍,包括it技術 管理 歷史 營銷 金融 心理等等,在讀某商學院mba一年後感覺有些東西還是要寫寫,除了能增加自己對某些知識和理論的理解外,也許其他人也碰巧想關注一下,了解這方面的東西。我所寫的都是一些簡單的概念層面的解釋,不會做太深入的闡述...