2017-05-10 34 views
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 (如果有區別)

+0

絕對是一個騙局。該死的。我搜查,但顯然,「處理」是錯誤的術語 – helt

回答

相關問題