Python - How do I convert “an OS-level handle to an open file” to a file object?

后端 未结 6 1553
谎友^
谎友^ 2021-02-01 00:57

tempfile.mkstemp() returns:

a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that fi

6条回答
  •  太阳男子
    2021-02-01 01:12

    I can't comment on the answers, so I will post my comment here:

    To create a temporary file for write access you can use tempfile.mkstemp and specify "w" as the last parameter, like:

    f = tempfile.mkstemp("", "", "", "w") # first three params are 'suffix, 'prefix', 'dir'...
    os.write(f[0], "write something")
    

提交回复
热议问题