Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Read handle concatenation (http://www.velocityreviews.com/forums/t704306-read-handle-concatenation.html)

kj 11-05-2009 03:24 PM

Read handle concatenation
 



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

Jon Clements 11-05-2009 03:33 PM

Re: Read handle concatenation
 
On Nov 5, 3:24*pm, kj <no.em...@please.post> wrote:
> 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?

Tim Chase 11-05-2009 04:06 PM

Re: Read handle concatenation
 
> 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




All times are GMT. The time now is 03:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.