How do I close the files from tempfile.mkstemp?

风格不统一 提交于 2019-12-01 02:44:18
import tempfile
import os
for idx in xrange(1024 + 1):
    outfd, outsock_path = tempfile.mkstemp()
    outsock = os.fdopen(outfd,'w')
    outsock.close()

Since mkstemp() returns a raw file descriptor, you can use os.close():

import os
from tempfile import mkstemp

for n in xrange(1024 + 1):
    f, path = mkstemp()
    # Do something with 'f'...
    os.close(f)

Use os.close() to close the file descriptor:

import os
from tempfile import mkstemp

# Open a file
fd, path = mkstemp()  

# Close opened file
os.close( fd )
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!