How to read file N lines at a time in Python? -


i need read big file reading @ n lines @ time, until eof. effective way of doing in python? like:

with open(filename, 'r') infile:     while not eof:         lines = [get next n lines]         process(lines) 

one solution list comprehension , slice operator:

with open(filename, 'r') infile:     lines = [line line in infile][:n] 

after lines tuple of lines. however, load complete file memory. if don't want (i.e. if file large) there solution using generator expression , islice itertools package:

from itertools import islice open(filename, 'r') infile:     lines_gen = islice(infile, n) 

lines_gen generator object, gives each line of file , can used in loop this:

for line in lines_gen:     print line 

both solutions give n lines (or fewer, if file doesn't have much).


Comments