Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Read handle concatenation

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


no.email at please

Nov 5, 2009, 7:24 AM

Post #1 of 3 (47 views)
Permalink
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
--
http://mail.python.org/mailman/listinfo/python-list


joncle at googlemail

Nov 5, 2009, 7:33 AM

Post #2 of 3 (43 views)
Permalink
Re: Read handle concatenation [In reply to]

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?
--
http://mail.python.org/mailman/listinfo/python-list


python.list at tim

Nov 5, 2009, 8:06 AM

Post #3 of 3 (43 views)
Permalink
Re: Read handle concatenation [In reply to]

> 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


--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.