我想更改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 (如果这有区别的话)
发布于 2017-05-10 05:47:14
使用os.close关闭由文件描述符表示的文件:
os.close(handle)https://stackoverflow.com/questions/43884364
复制相似问题