On Thu, 27 Dec 2007 18:49:06 -0800, byte8bits wrote:

> Wondering if someone would help me to better understand tempfile. I
> attempt to create a tempfile, write to it, read it, but it is not
> behaving as I expect. Any tips?

You need to seek to the part of the file you want to read:

>>> x = tempfile.TemporaryFile()
>>> x.read()  # file is empty to start with
''
>>> x.write('Nobody expects the Spanish Inquisition!')
>>> x.read()  # current file is at the end of the file, so nothing to read
''
>>> x.seek(0)  # move to the beginning of the file
>>> x.read()
'Nobody expects the Spanish Inquisition!'
>>> x.seek(7)
>>> x.write('EXPECTS')
>>> x.tell()  # where are we?
14L
>>> x.seek(0)
>>> x.read()
'Nobody EXPECTS the Spanish Inquisition!'


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to