如何使 Python 个临时文件和文件夹在定义的天数内持久存在?

How to make Python temporary files and folders persistent for a defined period of days?

我想使用 tempfile 模块来生成文件并将它们保存在 /tmp/myfolder/ 中并保存到某个时间(可能是几天)。我了解到 tempfile 会在 f.close() 为 运行 时立即删除文件。我只是不想让它自动删除。稍后将删除。 tempfile 这可能吗?或任何其他更好的方法?

您可以使用 this:

import tempfile
with tempfile.NamedTemporaryFile(delete=False, dir='/tmp/myfolder') as outfile:
    # ...

另一个解决方案是使用 tempfile.mkdtemp()