【原创】用python实现shell的tail操作

时间:2015-06-24 18:30:18   收藏:0   阅读:246

在工作过程中发现监控实时刷新文件时,不是那么的任性。

故结合shell中的tail,做了一个类似tail的python脚本。

详情如下:

 1 #!/usr/bin/env python
 2 #coding=utf-8
 3 import os,sys,time,getopt
 4 
 5 lastline=‘‘
 6 linelist=[]
 7 def getNewLine(filename, count):
 8     global lastline
 9     global linelist
10     cmd=tail -+count+ +filename
11     while(1):
12         #line5=os.popen(‘tail -5 speedy_result.txt‘).read()
13         newcount=0
14         line5=os.popen(cmd).read()
15         lines=line5.split(\n)
16         for line in lines:
17             if(line <= lastline): continue
18             newcount+=1
19             lastline=line
20             linelist=[]
21             linelist.append(line)
22 
23         time.sleep(0.5)
24         if(newcount<=0):
25             linelist=[]
26 
27         return linelist

 

原文:http://www.cnblogs.com/neujie/p/4597936.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!