Is it possible to let a virtual file created by cStringIO have a filename so that functions can read it by its filename?

Steven Howe howe.steven at gmail.com
Fri Jan 14 17:32:59 EST 2011


On 01/14/2011 12:51 PM, Chris Rebert wrote:
> On Fri, Jan 14, 2011 at 7:52 AM, Cun Zhang<apzc2529 at gmail.com>  wrote:
>> Hi,all
>> I hope use cStringIO to create virtual file, but my customed function which
>> is from a shared library imported by ctypes
>> just accepts a filename(string type) as parameter.
>>
>> So I'm wondering whether there is any method that make the virtual file
>> created by cStringIO like a normal file which have
>> a filename, so it can be called by my functions.
> That's not possible. (c)StringIO presents a file-like interface at the
> Python level, but under the covers, it's not implemented using
> anything like a normal file; thus, it doesn't have a presence on any
> filesystem. I would suggest using a temporary file
> (http://docs.python.org/library/tempfile.html ) for communicating with
> the C module, writing the contents of the StringIO object to the
> temporary file if necessary.
> (It's probably also possible to hack something together with FUSE, but
> it'd be a slow, platform-specific kludge.)
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com

However, as the only reason to have the cstringIO object have a name is
for you to open or close it. The rest of the functionality is the same, 
reading and writing too.


------------------
in a terminal ...
import cStringIO

ab = cStringIO.StringIO() # an output type, as it was call with nothing.
cd = cStringIO.StringIO( 'a filled buffer') # an input type.

type( ab ) == cStringIO.OutputType
True

type( cd ) == cStringIO.InputType
True

Working with these properties .... we get


Let's assume you have class with read and write ability,
which assumes opening a file or cStringIO object (could
extend to StringIO object with out much changing ).


class foo:
     def __init__( self ):
         """ Define some variables. House keeping. """
         self.readState = False
         self.writeState = False
         self.readObj = None
         self.writeObj = None

     def fooOpenWrite( self, fileobj ):
         if type( fileobj ) === StringType:
             self.writeObj = open( fileobj, 'wb' )
         elif type( fileobj ) == cStringIO.OutputType:
             self.writeObj = fileobj
         else:
             self.writeState = False
             return
         self.readState = True
         return True

     def fooOpenRead( self, fileobj ):
         if type( fileobj ) === StringType:
             self.readObj = open( fileobj, 'wb' )
         elif type( fileobj ) == cStringIO.OutputType:
             self.readObj = fileobj
         else:
             self.readState = False
             return
         self.readState = True
         return

     def fooRead( self ):
         for x in self.readObj:
             print x

     def fooWrite( self, str ):
         self.readObj.write( str )


Steven




More information about the Python-list mailing list