python处理大日志文件
本文实例为大家分享了python处理大日志文件的具体代码,供大家参考,具体内容如下
#coding=utf-8 importsys importtime classTail(): def__init__(self,file_name,callback=sys.stdout.write): self.file_name=file_name self.callback=callback deffollow(self,n=10): try: #打开文件 withopen(self.file_name)asf: self._file=f self._file.seek(0,2) #存储文件的字符长度 self.file_length=self._file.tell() #打印最后10行 self.showLastLine(n) #持续读文件打印增量 whileTrue: line=self._file.readline() ifline: self.callback(line) time.sleep(1) exceptException,e: print'打开文件失败,囧,看看文件是不是不存在,或者权限有问题' printe defshowLastLine(self,n): #一行大概100个吧这个数改成1或者1000都行 len_line=100 #n默认是10,也可以follow的参数传进来 read_len=len_line*n #用last_lines存储最后要处理的内容 whileTrue: #如果要读取的1000个字符,大于之前存储的文件长度 #读完文件,直接break ifread_len>self.file_length: self._file.seek(0) last_lines=self._file.read().split('\n')[-n:] break #先读1000个然后判断1000个字符里换行符的数量 self._file.seek(-read_len,2) last_words=self._file.read(read_len) #count是换行符的数量 count=last_words.count('\n') ifcount>=n: #换行符数量大于10很好处理,直接读取 last_lines=last_words.split('\n')[-n:] break #换行符不够10个 else: #break #不够十行 #如果一个换行符也没有,那么我们就认为一行大概是100个 ifcount==0: len_perline=read_len #如果有4个换行符,我们认为每行大概有250个字符 else: len_perline=read_len/count #要读取的长度变为2500,继续重新判断 read_len=len_perline*n forlineinlast_lines: self.callback(line+'\n') if__name__=='__main__': py_tail=Tail('test.txt') py_tail.follow(20)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。