我需要创建一个临时文件,其中包含一些测试内容:
def test_something():
tmp_path = make_temp_file('hello\nworld')
do_something(tmp_path)
Run Code Online (Sandbox Code Playgroud)
但我用来使文件在关闭时自动删除文件的以下代码:
def make_temp_file(text):
with tempfile.NamedTemporaryFile() as tmp_file:
tmp_file.write(text.encode('utf-8'))
tmp_file.flush()
return tmp_file.name
Run Code Online (Sandbox Code Playgroud)
所以,在do_something(tmp_path)文件中没有找到。如何防止关闭时删除 tmp 文件?测试完后我会手动删除它。