我想创建一个监视文件夹的模块。我写了一些代码:
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 方法。猫我怎么解决?
但是当我创建一个文件时,方法 process_IN_CREATE() 被调用。
操作系统是 Linux mint 13。
UPD:新代码