Python 未调用pyinotify方法

Python 未调用pyinotify方法,python,linux,inotify,pyinotify,Python,Linux,Inotify,Pyinotify,我想创建一个模块来监视文件夹。我写了一些代码: import os, pyinotify class FileWatcher: def start_watch(self, dir): wm = pyinotify.WatchManager() self.notifier = pyinotify.Notifier(wm, EventProcessor()) mask = pyinotify.IN_CREATE | pyinotify.IN_

我想创建一个模块来监视文件夹。我写了一些代码:

import os, pyinotify

class FileWatcher:
    def start_watch(self, dir):
        wm = pyinotify.WatchManager()
        self.notifier = pyinotify.Notifier(wm, EventProcessor())
        mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_DELETE_SELF | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
        wdd = wm.add_watch(dir, mask, rec=True)
        while True:
            self.notifier.process_events()
            if self.notifier.check_events():
                self.notifier.read_events()

    def stop_watch(self):
        self.notifier.stop()
        print ('\nWatcher stopped')

class EventProcessor(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        print('in CREATE')

    def process_IN_MODIFY(self, event):
        print('in MODIFY')

    def process_IN_DELETE(self, event):
        print('in delete')

    def process_IN_DELETE_SELF(self, event):
        print('in delete self')

    def process_IN_MOVED_FROM(self, event):
        print('in MOVED_FROM')

    def process_IN_MOVED_TO(self, event):
        print('in IN_MOVED_TO')

if __name__ == "__main__":
    watcher = FileWatcher()
    try:
        folder = "/home/user/Desktop/PythonFS"
        watcher.start_watch(folder)
    except KeyboardInterrupt:
        watcher.stop_watch()    
当我修改一个文件并将其删除时,从未调用方法process_IN_MODIFY和process_IN_DELETE。我怎么解决它

但是,当我创建一个文件时,调用了_create()中的方法process_

操作系统是Linux mint 13


UPD:新代码

请尝试以下代码。它与您的代码基本相同;我只补充了一点

f = FileWatcher()
f.start_watch('/tmp/test', None)
在末尾启动一个
文件监视程序
。确保目录
/tmp/test
存在,或者将该行更改为指向现有目录

如果
/tmp/test
中存在文件
foo
,并且如果我修改了此文件, 上面的程序打印

in create   # after modification
in modify   # after saving
in modify
in delete
现在,如果我删除该文件,程序将打印:

in delete


顺便说一句,一旦调用
f.start\u watch
,进程就会陷入一个
while True
循环,无法从中逃脱。即使从另一个线程调用
f.stop\u watch
,也不会以某种方式使您脱离这个while循环


如果您确实计划使用线程,则可能需要将
线程.Event
传递到
start\u watch
,并检查
while循环中的状态,以确定何时中断循环。

尝试以下代码。它与您的代码基本相同;我只补充了一点

f = FileWatcher()
f.start_watch('/tmp/test', None)
在末尾启动一个
文件监视程序
。确保目录
/tmp/test
存在,或者将该行更改为指向现有目录

如果
/tmp/test
中存在文件
foo
,并且如果我修改了此文件, 上面的程序打印

in create   # after modification
in modify   # after saving
in modify
in delete
现在,如果我删除该文件,程序将打印:

in delete


顺便说一句,一旦调用
f.start\u watch
,进程就会陷入一个
while True
循环,无法从中逃脱。即使从另一个线程调用
f.stop\u watch
,也不会以某种方式使您脱离这个while循环


如果您确实计划使用线程,您可能需要将
threading.Event
传递到
start\u watch
,并检查
while循环中的状态以确定何时中断循环。

嗯,此代码在语法上无效(缩进丢失,只需粘贴它,选择它,然后按
Ctrl+K
),它只定义类和函数,不调用它们中的任何一个。它是语法上的vallid代码。。方法应该从通知程序调用,因为我将EventProcessor的实例放在了pyinotify.notifier(…)的构造函数中。为什么里面有一些可调用的函数,代码实际上并没有调用它们。哦,对不起。代码的解析器工作不好。ctrl+k对我没有帮助。我改变了这一行。从另一个模块调用stop\u watch和start\u watch。这是简单的启动和停止。我想他们没有触及我的问题。嗯,这段代码在语法上是无效的(缩进缺失,只需粘贴它,选择它,然后按
Ctrl+K
),它只定义类和函数,不调用它们中的任何一个。它在语法上是vallid代码。。方法应该从通知程序调用,因为我将EventProcessor的实例放在了pyinotify.notifier(…)的构造函数中。为什么里面有一些可调用的函数,代码实际上并没有调用它们。哦,对不起。代码的解析器工作不好。ctrl+k对我没有帮助。我改变了这一行。从另一个模块调用stop\u watch和start\u watch。这是简单的启动和停止。我想他们没有触及我的问题。