【问题标题】:python watchdog file mod notificationpython看门狗文件mod通知
【发布时间】:2021-10-16 19:30:03
【问题描述】:

下面的python代码是我第一次尝试在文件被修改时通知,但是当文件被更改时没有任何反应。

我在这里错过了什么?

#!/usr/bin/python3

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print('file changed')


if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path='/Users/jeff/smb/storage/wsjt-x/wsjtx_log.adi', recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

注意:我发现这段代码在 Linux 上运行良好。它在我遇到问题的 macOS Big Sur 上。

【问题讨论】:

  • 相反,您在运行时是否遇到任何错误?对我来说它有效,我在运行时向我观察到的文件添加一行时调用了打印处理程序。
  • 没有错误。我运行脚本并在另一个 shell 中对观察到的文件进行更改,但从未调用 print 语句。
  • 注意:我发现这段代码在 Linux 上运行良好。它在我遇到问题的 macOS Big Sur 上。
  • 这是您的问题的stackoverflow.com/a/17586617/15978922 awnser 吗?
  • 不,仍然没有快乐。

标签: python watchdog


【解决方案1】:

在 cmets 中阐明您的环境后,以下 3 种方法之一可能会解决您的问题。

复制:在 Linux 上观看本地文件

在一个 shell A 中运行,同时使用另一个 shell B 添加到观察到的文件so.md,例如:

echo "---" >> so.md 

结果如下:

python3 so_watchdog.py so.md
file changed <FileModifiedEvent: event_type=modified, src_path='so.md', is_directory=False>
file changed <DirModifiedEvent: event_type=modified, src_path='', is_directory=True>

这是您的脚本稍作修改的版本(参见 cmets):

#!/usr/bin/python3

import time
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print('file changed', event)  # added event to see the difference of two events for a single modification


if __name__ == "__main__":
    if len(sys.argv) < 2:
       file = '/Users/jeff/smb/storage/wsjt-x/wsjtx_log.adi'
    else:
       file = sys.argv[1]
    # below only added the file as path
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path=file, recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

注意:这是在 Linux 上使用本地文件进行测试的。

查看共享文件 (CIFS/SMB)

由于您的路径 /Users/jeff/smb/storage/wsjt-x/wsjtx_log.adi 包含 smb 我假设您正在使用 SMB (Server Message Blocks) 协议(也称为 CIFS)在已安装的共享网络位置上观看文件(通用 Internet 文件系统)。

那么请关注官方文档,About using watchdog with CIFS:

当您想观察 CIFS 中的变化时,您需要明确告诉看门狗使用PollingObserver,也就是说,不要像上面的示例那样让看门狗决定合适的观察者,而是:

from watchdog.observers.polling import PollingObserver as Observe

所以你的导入语句需要修改。

在 MacOS 上本地观看文件

对于 MacOS,使用的底层文件系统事件 API 是 FSEvents 或 FreeBSD 的 kqueue,而不是 Linux 的 inotify

因此行为也可能会偏离。

【讨论】:

  • 与您编辑的脚本相同的空结果。我运行脚本并在另一个 shell 中对观察到的文件进行更改,但从未调用 print 语句。
  • 注意:我发现这段代码在 Linux 上运行良好。它在我遇到问题的 macOS Big Sur 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多