亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python 實時遍歷日志文件

 更新時間:2016年04月12日 13:31:44   作者:卑鄙的wo  
這篇文章主要介紹了python 實時遍歷日志文件 的相關資料,需要的朋友可以參考下

open 遍歷一個大日志文件

使用 readlines() 還是 readline() ?

總體上 readlines() 不慢于python 一次次調用 readline(),因為前者的循環(huán)在C語言層面,而使用readline() 的循環(huán)是在Python語言層面。

但是 readlines() 會一次性把全部數(shù)據(jù)讀到內存中,內存占用率會過高,readline() 每次只讀一行,對于讀取 大文件, 需要做出取舍。

如果不需要使用 seek() 定位偏移, for line in open('file') 速度更佳。

使用 readlines(),適合量級較小的日志文件

import os
import time
def check():
p = 
while True:
f = open("log.txt", "r+")
f = open("result.txt", "a+")
f.seek(p, )
#readlines()方法
filelist = f.readlines()
if filelist:
for line in filelist:
#對行內容進行操作
f.write(line)
#獲取當前位置,為下次while循環(huán)做偏移
p = f.tell()
print 'now p ', p
f.close()
f.close()
time.sleep()
if __name__ == '__main__':
check() 

使用 readline(),避免內存占用率過大

import os
import time
def check():
p = 
while True:
f = open("log.txt", "r+")
f = open("result.txt", "a+")
f.seek(p, )
#while readline()方法
while True:
l = f.readline()
#空行同樣為真
if l:
#對行內容操作
f.write(l)
else:
#獲取當前位置,作為偏移值
p = f.tell()
f.close()
f.close()
break
print 'now p', p
time.sleep()
if __name__ == '__main__':
check()

相關文章

  • PyTorch中torch.tensor()和torch.to_tensor()的區(qū)別

    PyTorch中torch.tensor()和torch.to_tensor()的區(qū)別

    在Pytorch中Tensor和tensor都用于生成新的張量,但二者并不相同,下面這篇文章主要給大家介紹了關于PyTorch中torch.tensor()和torch.to_tensor()區(qū)別的相關資料,需要的朋友可以參考下
    2023-01-01
  • 最新評論