If you want to create a temporary directory in Python3, you can easily use tempfile.TemporaryDirectory (). The following page is easy to understand how to use.
Create a temporary directory in Python and clean it up safely
As you can see on this page, I can't use tempfile.TemporaryDirectory ()
in Python2, so I investigated an alternative method.
During my research, I found the following article.
stack overflow / tempfile.TemporaryDirectory context manager in Python 2.7
If you use a library called backports.tempfile, you can write tempfile.TemporaryDirectory ()
as it is.
First, install the library.
#For pip
pip install backports.tempfile
#For pipenv
pipenv install backports.tempfile
#For poetry
poetry add backports.tempfile
Use it as follows.
import os
from backports import tempfile
#Temporary directory creation
with tempfile.TemporaryDirectory() as temp_dir:
print(os.path.exists(temp_dir))
You have successfully created a temporary directory!
In creating this article, I referred to the following pages. Thank you very much.
Recommended Posts