2015-05-29 185 views
5

问题:How do I watch a file for changes using Python?建议使用看门狗,但我发现它只能看到一个目录,而不是文件。 watchdog-test.py是监督员的示例脚本:如何查看文件,而不是使用Python进行更改的目录?

$ python watchdog-test.py ab_test_res.sh & 
[1] 30628 
[email protected]:~/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)。顺便说一句,我不想​​使用投票。

+1

什么操作系统?我不知道,但我认为Windows只支持观看目录。 –

+0

@ColonelThirtyTwo Linux。 – laike9m

+3

调查inotify http://linux.die.net/man/7/inotify –

回答

2

您可以通过观察文件所在的目录并仅响应更改影响文件的事件来观看带有看门狗的文件。像这样的东西会为你做:

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上测试这个,但它应该是不可知的。

相关问题