Making a copy (not reference) of a file handle, or starting stdin over at line 0

Peter Otten __peter__ at
Fri Aug 17 20:23:32 CEST 2007

Shawn Milochik wrote:

> How can I check a line (or two) from my input file (or stdin stream)
> and still be able to process all the records with my function?

One way:

from itertools import chain
firstline =
head = [firstline]

# loop over entire file
for line in chain(head, instream):

You can of course read more than one line as long as you append it to the
head list. Here's an alternative:

from itertools import tee
a, b = tee(instream)

for line in a:
    # determine file format,
    # break when done

# this is crucial for memory efficiency
# but may have no effect in implementations
# other than CPython
del a 

# loop over entire file
for line in b:
    # process line


More information about the Python-list mailing list