0
我想更改def a
以關閉由mkstemp
打開的句柄。但我失敗了。如何關閉tempfile.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))
我用蟒蛇蟒蛇2.7.13與Windows (如果有區別)
絕對是一個騙局。該死的。我搜查,但顯然,「處理」是錯誤的術語 – helt