I would like to read just one part (not chunks) from a txt-file (10GB) with lines and write them into another file. The size of the part should be exactly 25MB.
I have tried with linecache.getlines
, but it was not very exactly.
Thanks.
I would like to read just one part (not chunks) from a txt-file (10GB) with lines and write them into another file. The size of the part should be exactly 25MB.
I have tried with linecache.getlines
, but it was not very exactly.
Thanks.
A simple way to perform the split is to use read(), assuming each character is a byte.
for nameadd in range(10*1024/25):
f = open('fname.txt')
saveTxt = f.read(25*(1024**2))
fSave = open(str(nameadd)+'fname.txt','w')
fSave.write('%s',saveTxt)
It is already described here Lazy Method for Reading Big File in Python?
def read_in_chunks(file_object, chunk_size=25*1024*1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 25MB."""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
f = open('really_big_file.dat')
for piece in read_in_chunks(f):
process_data(piece)