TypeError: '_TemporaryFileWrapper' object is not an iterator
Antoon Pardon
antoon.pardon at rece.vub.ac.be
Fri Jul 29 04:43:12 EDT 2016
Below is a short program that illustrate the problem
It works with python2, whether you use the -c option or not.
It only works with python3 if you use the -c option.
The problem seems to come from my expectation that a file
is its own iterator and in python3 that is no longer true
for a NamedTemporaryFile.
Should this be considered a bug?
=========================================================================
from tempfile import NamedTemporaryFile
import sys
def main(tmp):
write = sys.stdout.write
if tmp:
tstfl = NamedTemporaryFile("w+", prefix = 'tmptest-')
else:
tstfl = open("Temporary", "w+")
for nr in range(10):
tstfl.write("This is line %d\n" % nr)
tstfl.seek(0)
try:
while True:
ln = next(tstfl)
write(ln)
except StopIteration:
pass
if __name__ == "__main__":
tmp = True
if len(sys.argv) > 1:
# if -c option is passed a normal file will be
# used instead of a NamedTemporaryFile
if sys.argv[1] == '-c':
tmp = False
main(tmp)
More information about the Python-list
mailing list