convert string to input stream

Titus Barik titus at barik.net
Tue Aug 31 12:32:20 EDT 2004


Hi,

I've used Google quite a bit in an attempt to find an answer to this
question, but have found no leads, possibly because I'm not sure I'm
wording my question properly. So I thought I'd try here.

Basically I have a function that accepts an input file stream as an
argument:

	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
functions in other languages that will take strings and convert them to
streams via IO Memory Buffers and such, but can't seem to find anything in
Python; or at least I don't know where to look.

One solution that I didn't like was to do the following:

	1. Open a temporary file.
	2. Write the string to that file.
	3. Read it back.

Any suggestions are appreciated.

Regards,


-- 
Titus Barik (titus at barik.net)
http://www.barik.net




More information about the Python-list mailing list