I’m able to create and write to a temp file, however when reading the file lines are empty. I confirmed temp file has content. Here is my code. Thanks
import tempfile
temp = tempfile.NamedTemporaryFile()
with open("~/somefile.txt") as inf:
for line in inf:
if line==line.lstrip():
temp.write(line)
line = str(temp.readline()).strip()
print line #nothing
You have to re-open (or rewind) the temp file before you can read from it:
Otherwise, the file pointer is positioned at the end of the file when you call
temp.readline().