import pyinotify class EventHandler(pyinotify.ProcessEvent): def __init__(self, path): self.path = path def process_IN_MODIFY(self, event): print("File modified:", event.pathname) wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm, EventHandler()) paths = ['/opt/tomcat/logs', '/home/temp'] for path in paths: wm.add_watch(path, pyinotify.IN_MODIFY, rec=True, auto_add=True) notifier.loop()报错: notifier = pyinotify.Notifier(wm, EventHandler()) TypeError: __init__() takes exactly 2 arguments (1 given)
时间: 2024-01-11 11:02:42 浏览: 200
pyinotify-0.8.9.tar.gz
这个错误提示说明 `EventHandler` 类的 `__init__` 方法需要传入 `path` 参数,但是在 `pyinotify.Notifier` 实例化时没有传入该参数,因此需要在实例化 `EventHandler` 时传入 `path` 参数,例如:
```python
import pyinotify
class EventHandler(pyinotify.ProcessEvent):
def __init__(self, path):
self.path = path
def process_IN_MODIFY(self, event):
print("File modified:", event.pathname)
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, EventHandler('/opt/tomcat/logs'))
wm.add_watch('/opt/tomcat/logs', pyinotify.IN_MODIFY, rec=True, auto_add=True)
notifier.loop()
```
注意,`wm.add_watch` 也需要改为仅传入一个路径参数。如果你想监控多个路径,可以在循环中分别创建 `EventHandler` 实例,并为每个实例传入相应的路径参数。
阅读全文