我想更改def a
以关闭由mkstemp
打开的句柄。但我失败了。
handle.close()
导致错误,因为句柄只是int
...
del handle
也不会改变行为
MWE:
import tempfile
import codecs
def a(json_content):
handle, file = tempfile.mkstemp(prefix="foobar-",suffix=".json")
write_to_file(json_content, file)
def write_to_file(text, filename):
with codecs.open(filename, 'w', 'utf-8', errors='ignore') as fp:
fp.write(unicode(text))
if __name__ == '__main__':
for i in range(50000):
a('{"foo":"bar", "iteration":%s}' %(i))
我使用带有Windows的anaconda python 2.7.13(如果这样做有所不同)