Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > concatenate file-like objects -> file-like object

Reply
Thread Tools

concatenate file-like objects -> file-like object

 
 
kgk
Guest
Posts: n/a
 
      07-11-2007

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

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      07-11-2007
On Tue, 10 Jul 2007 17:55:52 -0700, kgk wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate/De-Concatenate Carlos VHDL 10 10-24-2012 01:04 PM
cannot concatenate 'str' and 'list' objects Νικόλαος Κούρας Python 42 09-15-2012 11:11 PM
How to concatenate datetime.date object and datetime.time object Will Rocisky Python 1 08-05-2008 10:11 AM
TypeError: cannot concatenate 'str' and 'NoneType' objects thompson.marisa@gmail.com Python 9 12-20-2006 08:26 PM
concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object NotGiven Javascript 7 07-24-2003 11:44 AM



Advertisments