Search code examples
pythonfile-ioiterator

How to read file N lines at a time?


I need to read a big file by reading at most N lines at a time, until EOF. What is the most effective way of doing it in Python? Something like:

with open(filename, 'r') as infile:
    while not EOF:
        lines = [get next N lines]
        process(lines)

Solution

  • One solution would be a list comprehension and the slice operator:

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

    After this lines is tuple of lines. However, this would load the complete file into memory. If you don't want this (i.e. if the file could be really large) there is another solution using a generator expression and islice from the itertools package:

    from itertools import islice
    with open(filename, 'r') as infile:
        lines_gen = islice(infile, N)
    

    lines_gen is a generator object, that gives you each line of the file and can be used in a loop like this:

    for line in lines_gen:
        print line
    

    Both solutions give you up to N lines (or fewer, if the file doesn't have that much).