Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python查看文件而不是目录的更改?_Python_File_Watch_Python Watchdog - Fatal编程技术网

如何使用Python查看文件而不是目录的更改?

如何使用Python查看文件而不是目录的更改?,python,file,watch,python-watchdog,Python,File,Watch,Python Watchdog,问题:建议使用watchdog,但我发现它只能监视目录,而不能监视文件。是watchdog的示例脚本: $ 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 "/u

问题:建议使用watchdog,但我发现它只能监视目录,而不能监视文件。是watchdog的示例脚本:

$ 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
$python watchdog-test.py ab\u test\u res.sh&
[1] 30628
fbt@fbt64:~/laike9m$Traceback(最近一次呼叫最后一次):
文件“watchdog test.py”,第15行,在
observer.start()
文件“/usr/local/lib/python2.7/dist-packages/watchdog/observators/api.py”,第255行,开始
emitter.start()
文件“/usr/local/lib/python2.7/dist-packages/watchdog/utils/__-init__.py”,第111行,在开始处
self.on_thread_start()
文件“/usr/local/lib/python2.7/dist packages/watchdog/observators/inotify.py”,第121行,在on_thread_start中
self.\u inotify=InotifyBuffer(路径,self.watch.is\u递归)
文件“/usr/local/lib/python2.7/dist packages/watchdog/observators/inotify_buffer.py”,第35行,在__
self.\u inotify=inotify(路径,递归)
文件“/usr/local/lib/python2.7/dist packages/watchdog/observators/inotify_c.py”,第187行,在__
self.\u add\u dir\u watch(路径、递归、事件掩码)
文件“/usr/local/lib/python2.7/dist packages/watchdog/observators/inotify_c.py”,第363行,位于“添加目录”监视中
raise OSError('路径不是目录')
OSError:路径不是目录

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

您可以通过监视文件所在的目录并仅响应影响文件的更改事件,使用watchdog监视文件。像这样的东西可以帮你:

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上测试它,但它应该与操作系统无关。

什么操作系统?我不确定,但我认为Windows只支持监视目录。@ColonelThirtyTwo Linux.look into inotify我可以按照您在perl中的要求执行相同的操作。我试图不断地查找文件以查找任何更改,但每次查找之间都会有一个睡眠。这将有助于减少系统负载。如果您想要我的perl解决方案,我可以提供给您。@shivams这不是轮询,但我正在寻找另一种方法。