擅长:python、mysql、java
<p>smarx的应答通过指定<code>path</code>打开文件。但是,更容易指定<code>fd</code>。在这种情况下,上下文管理器会自动关闭文件描述符:</p>
<pre><code>from tempfile import mkstemp
fd, path = mkstemp()
# use a context manager to open (and close) file descriptor fd (which points to path)
with open(fd, 'w') as f:
f.write('TEST\n')
# This causes the file descriptor to be closed automatically
</code></pre>