Read handle concatenation

K

kj

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.)

The only way I know to do this from scratch is straightforward but
tedious, so I thought I'd better ask to see if there's already some
module that would facilitate this task.

TIA!

kynn
 
J

Jon Clements

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.)

The only way I know to do this from scratch is straightforward but
tedious, so I thought I'd better ask to see if there's already some
module that would facilitate this task.

TIA!

kynn

Does the fileinput module do what you want?
 
T

Tim Chase

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top