如何在 Python 中创建自删除临时目录
从 Python 3.2 开始你可以像这样使用 tmpfile.TemporaryDirectory:
tempdir_example.py
from tempfile import TemporaryDirectory
with TemporaryDirectory(prefix="myapp-") as tmpdir:
print(tmpdir) # 例如 "/tmp/myapp-fevhzh93"
# 一旦你在 "with" 块之外,目录将被删除!如果你被困于使用较旧的 Python 版本,请查看如何在 Python 中创建临时目录。
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow