gpt4 book ai didi

python - pyinotify 方法未调用

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:18 24 4
gpt4 key购买 nike

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

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:新代码

最佳答案

试试下面的代码。它与您的代码基本相同;我只添加了

f = FileWatcher()
f.start_watch('/tmp/test', None)

最后启动一个FileWatcher。确保目录 /tmp/test 存在,或者更改该行以指向一个存在的目录。

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

in create   # after modification
in modify # after saving
in modify
in delete

现在如果我删除文件,程序会打印:

in delete

import os
import pyinotify


class FileWatcher:
notifier = None

def start_watch(self, dir, callback):
wm = pyinotify.WatchManager()
self.notifier = pyinotify.Notifier(wm, EventProcessor(callback))
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()


class EventProcessor(pyinotify.ProcessEvent):
def __init__(self, callback):
self.event_callback = callback

def process_IN_CREATE(self, event):
# if self.event_callback is not None:
# self.event_callback.on_file_created(os.path.join(event.path,
# event.name))
print('in create')

def process_IN_MODIFY(self, event):
# if self.event_callback is not None:
# self.event_callback.on_file_modifed(os.path.join(event.path,
# event.name))
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 moved to')


f = FileWatcher()
f.start_watch('/tmp/test', None)

顺便说一下,一旦你调用了f.start_watch,这个过程就陷入了一个while True循环,它无法从中逃脱。即使从另一个线程调用 f.stop_watch 也不会以某种方式使您脱离此 while 循环。

如果您确实计划使用线程,您可能需要将一个 threading.Event 传递给 start_watch,并在 while-loop< 中检查它的状态 决定何时跳出循环。

关于python - pyinotify 方法未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15642578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com