concatenate file-like objects -> file-like object

K

kgk

I would like to concatenate several file-like objects
to create a single file-like object. I've looked at fileinput,
however
this returns a fileinput object that is not very file-like.

something like
# a has 50 bytes, and b has 100 bytes
f = FileList (open('a'), open('b'))
f.read (100) # read 50 bytes from a and 50 from b

My interest is in passing several files to an incremental parser
as if they came from a single file. I would rather not load them
in memory using StringIO and the parser reads only from file-like
objects.

Any pointers appreciated
Kris
 
M

Marc 'BlackJack' Rintsch

I would like to concatenate several file-like objects
to create a single file-like object. I've looked at fileinput,
however
this returns a fileinput object that is not very file-like.

something like
# a has 50 bytes, and b has 100 bytes
f = FileList (open('a'), open('b'))
f.read (100) # read 50 bytes from a and 50 from b

My interest is in passing several files to an incremental parser
as if they came from a single file. I would rather not load them
in memory using StringIO and the parser reads only from file-like
objects.

Then program a file like object yourself. Something like this (untestet):

class FileList(object):
def __init__(self, files):
self.files = reversed(files)
self.current_file = self.files.pop()

def read(size):
result = ''
while self.files:
data = self.current_file.read(size)
result += data
if len(data) != size:
self.current_file = self.files.pop()
size = size - len(data)
return result

Ciao,
Marc 'BlackJack' Rintsch
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top