ddd ddd - 4 years ago 68
Python Question

How to read lots of line from file at once

I want to generate a bunch of files based on a template. The template has thousands of lines. For each of the new files, only top 5 lines are different. What is the best way of reading all the lines but first 5 at once instead of read the whole file in line by line?

Answer Source

One approach would be to create a list of the 5 first lines, and read the rest in a big buffer:

with open("input.txt") as f:
    first_lines = [f.readline() for _ in range(5)]
    rest_of_lines = f.read()

or more symmetrical for the first part: create 1 small buffer with the 5 lines:

first_lines = "".join([f.readline() for _ in range(5)])

As an alternative, from a purely I/O point of view, the quickest would be

with open("input.txt") as f:
    lines = f.read()

and use a line split generator to read the 5 first lines (splitlines() would be disastrous in terms of memory copy, find an implementation here)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download