convert string to input stream

Alex Martelli aleaxit at yahoo.com
Wed Sep 1 07:26:23 EDT 2004


Titus Barik <titus at barik.net> wrote:
   ...
>       handle = open(SOME_FILE)
>       my_function(handle)
> 
> But occasionally, I end up with a string ("Hello world") that I need to
> convert to a stream in order to pass it into this function. Perhaps
> something like:
> 
>       fake_handle = convert_to_handle("Hello world!")
> 
> so that it appears to this function to be a file handle. I've seen

def convert_to_handle(somestring):
    import cStringIO
    return cStringIO.StringIO(somestring)


Alex



More information about the Python-list mailing list