Understanding tempfile.TemporaryFile
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Thu Dec 27 22:27:15 EST 2007
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
More information about the Python-list
mailing list