【发布时间】:2013-08-20 17:34:00
【问题描述】:
我想监控一个目录,该目录有子目录,子目录中有一些带有.md 的文件。 (可能还有一些其他的文件,比如*.swp...)
我只想监控 .md 文件,我已经阅读了文档,并且只有一个 ExcludeFilter,并且在问题中:https://github.com/seb-m/pyinotify/issues/31 说,只能过滤而不是文件。
现在我要做的是过滤process_* 函数以通过fnmatch 检查event.name。
那么如果我只想监控指定后缀的文件,有没有更好的办法呢?谢谢。
这是我写的主要代码:
!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyinotify
import fnmatch
def suffix_filter(fn):
suffixes = ["*.md", "*.markdown"]
for suffix in suffixes:
if fnmatch.fnmatch(fn, suffix):
return False
return True
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
if not suffix_filter(event.name):
print "Creating:", event.pathname
def process_IN_DELETE(self, event):
if not suffix_filter(event.name):
print "Removing:", event.pathname
def process_IN_MODIFY(self, event):
if not suffix_filter(event.name):
print "Modifing:", event.pathname
def process_default(self, event):
print "Default:", event.pathname
【问题讨论】: