[Tutor] re-reading file-like objects

Alan Gauld alan.gauld at btinternet.com
Tue Oct 10 00:46:05 CEST 2006

> For this simple example, this solution is fine, and I think the 
> solution
> proposed by Alan (StringIO) is not going to add anything. Would it 
> be faster?

Probably not, the StringIO solution is just closer to how you 
described the problem, but also provides a fuller set of file-like 
methods, including seek():

>>> import StringIO
>>> s = StringIO.StringIO()
>>> s.write('''
... here is some text
... that you can see''')
>>> s.read()   # at eof
>>> s.seek(0)
>>> s.read()   # at start of file
'\nhere is some text\nthat you can see'


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list