Python write in mkstemp() file

前端 未结 3 2423
鱼传尺愫
鱼传尺愫 2021-02-20 01:26

I am creating a tmp file by using :

from tempfile import mkstemp

I am trying to write in this file :

tmp_file = mkstemp()
file          


        
3条回答
  •  情深已故
    2021-02-20 02:24

    mkstemp() returns a tuple with a file descriptor and a path. I think the issue is that you're writing to the wrong path. (You're writing to a path like '(5, "/some/path")'.) Your code should look something like this:

    from tempfile import mkstemp
    
    fd, path = mkstemp()
    
    # use a context manager to open the file at that path and close it again
    with open(path, 'w') as f:
        f.write('TEST\n')
    
    # close the file descriptor
    os.close(fd)
    

提交回复
热议问题