By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

StringIO.readline() returns ''

P: n/a
Max
I'm using StringIO for the first time (to buffer messages recieved from
a socket). I thought it would be a simple matter of writing the stuff to
the buffer and then calling readline, but that doesn't seem to work:
buf = StringIO.StringIO()
buf.write("Foo\n")
buf.write("Bar\n")
buf.flush()
buf.readline()

''

I expected buf.readline() to return 'Foo\n'. What am I doing wrong?

--Max
Apr 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Unknown" wrote:
I'm using StringIO for the first time (to buffer messages recieved from
a socket). I thought it would be a simple matter of writing the stuff to
the buffer and then calling readline, but that doesn't seem to work:
>>> buf = StringIO.StringIO()
>>> buf.write("Foo\n")
>>> buf.write("Bar\n")
>>> buf.flush()
>>> buf.readline() ''

I expected buf.readline() to return 'Foo\n'. What am I doing wrong?


you forgot to rewind the file:
buf = StringIO.StringIO()
buf.write("Foo\n")
buf.write("Bar\n")
buf.seek(0)
buf.readline()

'Foo\n'

</F>

Apr 9 '06 #2

P: n/a
Max
Fredrik Lundh wrote:
you forgot to rewind the file:

Thank you.

</F>


--Max
Apr 9 '06 #3

P: n/a
You can also just type buf.getvalue() which returns the current
StringIO buffer as a python string, without the rewinding

Apr 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.