I am trying to create a temporary file that I write in some lines from another file and then make some objects from the data. I am not sure how to find and open the temp file s
You've got a scope problem; the file tmp
only exists within the scope of the with
statement which creates it. Additionally, you'll need to use a NamedTemporaryFile
if you want to access the file later outside of the initial with
(this gives the OS the ability to access the file). Also, I'm not sure why you're trying to append to a temporary file... since it won't have existed before you instantiate it.
Try this:
import tempfile
tmp = tempfile.NamedTemporaryFile()
# Open the file for writing.
with open(tmp.name, 'w') as f:
f.write(stuff) # where `stuff` is, y'know... stuff to write (a string)
...
# Open the file for reading.
with open(tmp.name) as f:
for line in f:
... # more things here