2014-01-21 32 views

回答

3

這是你會怎麼做(用發電機):

import time 

def follow(syslog_file): 
    syslog_file.seek(0,2) # Go to the end of the file 
    while True: 
     line = syslog_file.readline() 
     if not line: 
      time.sleep(0.1) # Sleep briefly 
      continue 
     yield line 

它像 '尾巴-f'。代碼取自:http://www.dabeaz.com/generators/Generators.pdf(第39頁)。此外,類似的SO問題:

Read from a log file as it's being written using python

+1

縮進是錯誤的'yield' – fileinster