Re: Is it possible to let a virtual file created by cStringIO havea filename so that functions can r

Discussion in 'Python' started by Steven Howe, Jan 14, 2011.

  1. Steven Howe

    Steven Howe Guest

    On 01/14/2011 12:51 PM, Chris Rebert wrote:
    > On Fri, Jan 14, 2011 at 7:52 AM, Cun Zhang<> 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
    Steven Howe, Jan 14, 2011
    #1
    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. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,003
    Travis Murray
    Aug 24, 2004
  2. Luigi Donatello Asero

    Let or not let the text float

    Luigi Donatello Asero, Jan 15, 2004, in forum: HTML
    Replies:
    6
    Views:
    457
    Steve R.
    Jan 15, 2004
  3. Replies:
    1
    Views:
    1,434
    Roland de Ruiter
    Jun 15, 2006
  4. John Goche
    Replies:
    10
    Views:
    723
    Marcus Kwok
    Dec 8, 2006
  5. Stefano Crocco
    Replies:
    0
    Views:
    132
    Stefano Crocco
    Nov 14, 2008
Loading...

Share This Page