Why does StringIO discard its initial value?

jepler at unpythonic.net jepler at unpythonic.net
Sun Apr 10 14:03:51 EDT 2005


Maybe this short interactive session can give you an idea why.

>>> from StringIO import StringIO
>>> b = StringIO("123456789")
>>> b.tell()
0
>>> b.write("abc")
>>> b.getvalue()
'abc456789'
>>> b.tell()
3

StringIO seems to operate like a file opened with "r+" (If I've got my modes
right): it is opened for reading and writing, and positioned at the beginning.
In my example, the write of 3 bytes overwrites the first 3 bytes of the file
and leaves the rest intact.  In your example your first write overwrote the
whole initial contents of the file, so you couldn't notice this effect.

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050410/06507212/attachment.sig>


More information about the Python-list mailing list