pyinotify ERROR] add_watch: cannot watch --foreground WD=-1, Errno=No such file or directory (ENOENT)
时间: 2024-04-26 11:24:09 浏览: 19
这个错误通常是因为在使用Pyinotify监视的目录不存在或者无法访问。根据错误信息,它似乎是在尝试监视一个叫做 "--foreground" 的目录,而这个目录不存在,导致无法添加监视。
解决这个问题的方法是检查你的代码,看看是否正确地指定了要监视的目录。如果指定的是一个具体的目录,那么请确保这个目录存在,并且你有足够的权限访问这个目录。如果你是使用命令行参数的方式来指定要监视的目录,那么请检查命令行参数是否正确。
另外,根据错误信息,它似乎是在尝试将 "--foreground" 作为目录名来添加监视,这显然是不正确的。请确保你正确地指定了要监视的目录,并且不要将无关的字符串作为目录名来添加监视。
相关问题
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) paths = ['/opt/tomcat/logs', '/home/temp'] for path in paths: wm.add_watch(path, pyinotify.IN_MODIFY, rec=True, auto_add=True, event_class=EventHandler(path)) notifier.loop(event_loop=pyinotify.EventLoop(event_class=EventHandler(path))) 报错:wm.add_watch(path, pyinotify.IN_MODIFY, rec=True, auto_add=True, event_class=EventHandler(path)) TypeError: add_watch() got an unexpected keyword argument 'event_class'
根据Pyinotify文档,`add_watch()`方法没有`event_class`参数。您可以尝试修改如下代码:
```
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()
```
在这个示例中,我们将`EventHandler`作为参数传递给`Notifier`的构造函数,而不是在`add_watch()`中使用`event_class`参数。
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)
这个错误提示说明 `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` 实例,并为每个实例传入相应的路径参数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)