You must reopen (or rewind) the temporary file before you can read it:
import tempfile temp = tempfile.NamedTemporaryFile() with open("~/somefile.txt") as inf: for line in inf: if line==line.lstrip(): temp.write(line) temp.seek(0) # <=============== ADDED line = str(temp.readline()).strip() print line
Otherwise, the file pointer is placed at the end of the file when you call temp.readline()
.
source share