File objects (be they temporary or otherwise) cannot be read multiple times without re-positioning the file position back to the start.
Your options are:
- To reopen the file multiple times, creating multiple file objects for the same file.
- To rewind the file object before each read.
To reopen the file, use a NamedTemporaryFile
and use a regular open()
call to re-open the same filename several times. You probably will want to pass delete=False
to the constructor, especially on Windows, to be able to do this.
To rewind, call .seek(0)
on the file object.