擅长:python、mysql、java
<p>完整的例子。</p>
<pre><code>import tempfile
with tempfile.NamedTemporaryFile() as temp:
temp.write('Some data')
if should_call_some_python_function_that_will_read_the_file():
temp.seek(0)
some_python_function(temp)
elif should_call_external_command():
temp.flush()
subprocess.call(["wc", temp.name])
</code></pre>
<p><strong>更新</strong>:如注释中所述,这在windows中可能不起作用。对windows使用<a href="https://stackoverflow.com/a/5344603">this</a>解决方案</p>