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

后端 未结 6 1546
谎友^
谎友^ 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:18

    You can use

    os.write(tup[0], "foo\n")
    

    to write to the handle.

    If you want to open the handle for writing you need to add the "w" mode

    f = os.fdopen(tup[0], "w")
    f.write("foo")
    

提交回复
热议问题