Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #112007

Re: TypeError: '_TemporaryFileWrapper' object is not an iterator

From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
Newsgroups comp.lang.python
Subject Re: TypeError: '_TemporaryFileWrapper' object is not an iterator
Date 2016-07-29 10:43 +0200
Message-ID <mailman.17.1469781843.6033.python-list@python.org> (permalink)
References <5798BECB.8030800@rece.vub.ac.be> <579B1720.9000103@rece.vub.ac.be>

Show all headers | View raw


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)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: TypeError: '_TemporaryFileWrapper' object is not an iterator Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-07-29 10:43 +0200

csiph-web