Read handle concatenation

Tim Chase python.list at tim.thechases.com
Thu Nov 5 17:06:05 CET 2009


> I want to be able to take two or more open read handles and
> concatenate them into an object that behaves like a regular read
> handle (i.e. a file object open for reading), but behind the scenes
> it reads from the concatenated handles in sequence.  I.e. I want
> the read-handle equivalent of the standard Unix utility cat.  (The
> reason I can't use subprocess and cat for this is that I want to
> concatenate read handles that do not necessarily come from files.)

Sounds like itertools.chain would do what you want:

   for line in itertools.chain(
       file('a.txt'),
       file('b.txt'),
       ):
     do_something(line)

or more generically if you have a list of file objects:

   lst = [file('a.txt'), file('b.txt'), ...]
   for line in itertools.chain(*lst):
     do_something(line)

-tkc





More information about the Python-list mailing list