[Python] 纯文本查看 复制代码
logfile=’access.log’
command=’tail -f ‘+logfile+’|grep “timeout”‘
popen=subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
while True:
line=popen.stdout.readline().strip()
print line
[Python] 纯文本查看 复制代码
import time
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
if __name__ == ‘__main__’:
logfile = open(“access-log”,”r”)
loglines = follow(logfile)
for line in loglines:
print line