Reputation: 39578
To me the most idiomatic way of calling tempfile.mkstemp()
would be as:
with tempfile.mkstemp() as fd, filename:
pass
however, this obviously(?) raises AttributeError: __exit__
Calling os.close(fd)
explicitly using try-finally is an easy way to solve this, but feels like violation of There should be one-- and preferably only one --obvious way to do it.
Is there a way to "fix" this in tempfile
or is there a rationale why this has been implemented this way?
Upvotes: 12
Views: 6832
Reputation: 4314
The workings of the with
statement is defined in PEP 343, including its so called context management protocol:
This PEP proposes that the protocol consisting of the enter() and exit() methods be known as the "context management protocol", and that objects that implement that protocol be known as "context managers".
mkstemp
does not return a context manager, that is an object which implements the __enter__
and __exit__
methods, and is therefore not compatible.
An obvious workaround is to create a wrapper class that implements the context manager protocol.
Upvotes: 7
Reputation: 91109
In the tempfile
module there are other, better suited ways to create a temporary file, such as TemporaryFile()
and others.
Especially, if you don't want the file to be deleted, use NamedTemporaryFile()
, giving it the delete=False
argument.
Upvotes: 6