python tail 文件的变化

创建日期: 2022-11-08 18:02 | 作者: 风波 | 浏览次数: 15 | 分类: Python

阻塞模式

import subprocess
f = subprocess.Popen(['tail','-F',filename], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
while True:
    line = f.stdout.readline()
    print line

非阻塞模式

import time
import subprocess
import select

f = subprocess.Popen(['tail','-F',filename], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p = select.poll()
p.register(f.stdout)

while True:
    if p.poll(1):
        print f.stdout.readline()
    time.sleep(1)

来源:https://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python

15 浏览
8 爬虫
0 评论