How do I close the files from tempfile.mkstemp?

后端 未结 3 1515
灰色年华
灰色年华 2021-01-07 16:46

On my machine Linux machine ulimit -n gives 1024. This code:

from tempfile import mkstemp

for n in xrange(1024 + 1):
    f, path =         


        
相关标签:
3条回答
  • 2021-01-07 17:30
    import tempfile
    import os
    for idx in xrange(1024 + 1):
        outfd, outsock_path = tempfile.mkstemp()
        outsock = os.fdopen(outfd,'w')
        outsock.close()
    
    0 讨论(0)
  • 2021-01-07 17:37

    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 )
    
    0 讨论(0)
  • 2021-01-07 17:49

    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)
    
    0 讨论(0)
提交回复
热议问题