时间:2021-07-01 10:21:17 帮助过:19人阅读
import time
def tail(filepath):
with open(filepath,‘rb‘) as f:
f.seek(0,2)
while True:
line=f.readline()
if line:
yield line
else:
time.sleep(0.05)
def grep(lines,pattern):
for line in lines:
line=line.decode(‘utf-8‘)
if pattern in line:
yield line
lines=grep(tail(‘access.log‘),‘404‘)
for line in lines:
print(line)
模拟管道,实现功能:tail -f access.log | grep '404'
标签:实现 line yield code lines imp pre for gpo