2015-05-29 13 views
5

Câu hỏi: How do I watch a file for changes using Python? đề xuất sử dụng cơ quan giám sát, nhưng tôi thấy nó chỉ có thể xem thư mục chứ không phải tệp. watchdog-test.py là tập lệnh mẫu của cơ quan giám sát:Làm cách nào để xem tệp, chứ không phải thư mục cho các thay đổi khi sử dụng 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 

Vậy giải pháp nào là tốt nhất? Tôi đang sử dụng Linux (Ubuntu 12.04). BTW Tôi không muốn sử dụng bỏ phiếu.

+1

Hệ điều hành nào? Tôi không biết chắc chắn, nhưng tôi nghĩ Windows chỉ hỗ trợ xem thư mục. –

+0

@ColonelThirtyTwo Linux. – laike9m

+3

nhìn vào inotify http://linux.die.net/man/7/inotify –

Trả lời

2

Bạn có thể xem tệp có cơ quan giám sát bằng cách xem thư mục chứa tệp và chỉ phản hồi để thay đổi các sự kiện ảnh hưởng đến tệp của bạn. Một cái gì đó như thế này sẽ làm điều đó cho bạn:

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) 

Tôi chỉ có thể kiểm tra điều này trên cửa sổ, nhưng nó sẽ là os thuyết bất khả tri.

Các vấn đề liên quan