Python - tempfile.TemporaryFile cannot be read; why? Python - tempfile.TemporaryFile cannot be read; why? python python

Python - tempfile.TemporaryFile cannot be read; why?


You must put

f.seek(0)

before trying to read the file (this will send you to the beginning of the file), and

f.seek(0, 2)

to return to the end so you can assure you won't overwrite it.


read() does not return anything because you are at the end of the file. You need to call seek() first before read() will return anything. For example, put this line in front of the first read():

f.seek(-10, 1)

Of course, before writing again, be sure to seek() to the end (if that is where you want to continue writing to).