Title: tempfile.mkstemp() | Documentation Error
Type: behavior Stage:
Components: Documentation Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Howard Waterfall, docs@python, steven.daprano
Priority: normal Keywords:

Created on 2020-04-05 00:39 by Howard Waterfall, last changed 2020-04-05 01:52 by steven.daprano.

Messages (2)
msg365805 - (view) Author: Howard Waterfall (Howard Waterfall) Date: 2020-04-05 00:39
The documentation for tempfile.mkstemp() says:
returns a tuple containing an OS-level handle to an open file (as would be returned by and the absolute pathname of that file, in that order.

I don't believe this is correct. It should say:
returns a tuple containing an OS-level file descriptor and the absolute pathname of that file, in that order.

I say this because this works:
    file_descriptor, uri = tempfile.mkstemp()
    open_file = os.fdopen(file_descriptor, 'w')
    open_file.write("hello world")

but this raises an error:
    open_file, uri = tempfile.mkstemp()
    open_file.write("hello world")

    Traceback (most recent call last):
    File "", line 78, in <module>
    File "", line 74, in main
        open_file.write("hello world")
    AttributeError: 'int' object has no attribute 'write'
msg365809 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-04-05 01:52
I think you may have misread the documentation. It says:

"an OS-level handle to an open file"

which is what you call a file descriptor. Not a file object, which is what you get by calling the builtin `open`, but a file handle like you get from calling `` (as stated).

So I believe that the documentation is correct, but given that at least one person misunderstood it, perhaps it could do with improvement.

Do you have any suggestion for improvement?
Date User Action Args
2020-04-05 01:52:15steven.dapranosetnosy: + steven.daprano
messages: + msg365809
2020-04-05 00:39:24Howard Waterfallcreate