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

Peter Otten __peter__ at web.de
Fri Aug 17 14:23:32 EDT 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 = instream.next()
head = [firstline]

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


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


Peter




More information about the Python-list mailing list