我們在處理小的文本文件時一般使用.read()、.readline() 和 .readlines()方法,但是當(dāng)我們的文件有2個G穗椅,5個G甚至更大時,用這些方法內(nèi)存就直接爆掉了奶栖。
對一般文件匹表,如果文件很小,read()一次性讀取最方便宣鄙;如果不能確定文件大小袍镀,反復(fù)調(diào)用read(size)比較保險;如果是配置文件冻晤,調(diào)用readlines()最方便苇羡。
讀取大文件方法:
一、Read In Chunks
把大文件分成小塊來讀
def read_in_chunks(filePath, chunk_size=1024*1024):
"""
Lazy function (generator) to read a file piece by piece.
Default chunk size: 1M
You can set your own chunk size
"""
file_object = open(filePath)
while True:
chunk_data = file_object.read(chunk_size)
if not chunk_data:
break
yield chunk_data
if __name__ == "__main__":
filePath = './path/filename'
for chunk in read_in_chunks(filePath):
process(chunk) # <do something with chunk>
二鼻弧、Using with open()
with語句打開和關(guān)閉文件设江,包括拋出一個內(nèi)部塊異常锦茁。for line in f文件對象f視為一個迭代器,會自動的采用緩沖IO和內(nèi)存管理叉存,所以你不必擔(dān)心大文件码俩。
#If the file is line based
with open(...) as f:
for line in f:
process(line) # <do something with line>
三、fileinput處理
import fileinput
for line in fileinput.input(['sum.log']):
print line
參考:
http://www.zhidaow.com/post/python-read-big-file
https://www.cnblogs.com/wulaa/p/7852592.html
f = open(filename,'r')
f.read()
#1:
while True:
block = f.read(1024)
if not block:
break
#2:
while True:
line = f.readline()
if not line:
break
#3:
for line in f.readlines():
pass
#4:
with open(filename,'r') as file:
for line in file:
pass
#5:the second line
import linecache
txt = linecache.getline(filename,2)