【问题标题】:How do I watch a file, not a directory for changes using Python?如何使用 Python 查看文件而不是目录以进行更改?
【发布时间】:2015-08-12 10:22:21
【问题描述】:

问题:How do I watch a file for changes using Python? 建议使用看门狗,但我发现它只能看目录,不能看文件。 watchdog-test.py 是看门狗的示例脚本:

$ python watchdog-test.py ab_test_res.sh &
[1] 30628
fbt@fbt64:~/laike9m$ Traceback (most recent call last):
  File "watchdog-test.py", line 15, in <module>
    observer.start()
  File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/api.py", line 255, in start
    emitter.start()
  File "/usr/local/lib/python2.7/dist-packages/watchdog/utils/__init__.py", line 111, in start
    self.on_thread_start()
  File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/inotify.py", line 121, in on_thread_start
    self._inotify = InotifyBuffer(path, self.watch.is_recursive)
  File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/inotify_buffer.py", line 35, in __init__
    self._inotify = Inotify(path, recursive)
  File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/inotify_c.py", line 187, in __init__
    self._add_dir_watch(path, recursive, event_mask)
  File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/inotify_c.py", line 363, in _add_dir_watch
    raise OSError('Path is not a directory')
OSError: Path is not a directory

那么最好的解决方案是什么?我正在使用 Linux(Ubuntu 12.04)。顺便说一句,我不想​​使用轮询。

【问题讨论】:

  • 什么操作系统?我不确定,但我认为 Windows 只支持观看目录。
  • @ColonelThirtyTwo Linux。
  • 我能够按照你在 perl 中的要求做同样的事情。我尝试不断寻找文件以进行任何更改,但在每次寻找之间都休息一下。这将有助于减少系统负载。如果你想要我的 perl 解决方案,我可以提供给你。
  • @shivams 这不是轮询,但我正在寻找另一种方式。

标签: python file watch python-watchdog


【解决方案1】:

您可以通过监视文件所在的目录并仅响应影响您的文件的更改事件来使用看门狗监视文件。这样的事情会为你做的:

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

class FileModifiedHandler(FileSystemEventHandler):

    def __init__(self, path, file_name, callback):
        self.file_name = file_name
        self.callback = callback

        # set observer to watch for changes in the directory
        self.observer = Observer()
        self.observer.schedule(self, path, recursive=False)
        self.observer.start()
        self.observer.join()

    def on_modified(self, event): 
        # only act on the change that we're looking for
        if not event.is_directory and event.src_path.endswith(self.file_name):
            self.observer.stop() # stop watching
            self.callback() # call callback


from sys import argv, exit

if __name__ == '__main__':

    if not len(argv) == 2:
        print("No file specified")
        exit(1)

    def callback():
        print("FILE WAS MODIFED")

    FileModifiedHandler('.', argv[1], callback)

我只能在 Windows 上进行测试,但它应该与操作系统无关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-08
    • 2022-01-22
    • 2015-11-22
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2016-02-02
    相关资源
    最近更新 更多