Read handle concatenation

Discussion in 'Python' started by kj, Nov 5, 2009.

  1. kj

    kj Guest

    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
     
    kj, Nov 5, 2009
    #1
    1. Advertising

  2. kj

    Jon Clements Guest

    On Nov 5, 3:24 pm, kj <> 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?
     
    Jon Clements, Nov 5, 2009
    #2
    1. Advertising

  3. kj

    Tim Chase Guest

    > 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
     
    Tim Chase, Nov 5, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf Hildebrandt

    type conversion and concatenation

    Ralf Hildebrandt, Jul 23, 2003, in forum: VHDL
    Replies:
    3
    Views:
    6,695
  2. walala
    Replies:
    3
    Views:
    4,813
    walala
    Sep 18, 2003
  3. Dietrich
    Replies:
    1
    Views:
    650
    Joe Smith
    Jul 22, 2004
  4. Leon
    Replies:
    2
    Views:
    537
  5. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    454
    hanumizzle
    Oct 6, 2006
Loading...

Share This Page