I want to use Temporary Directory with Python2

I want to use Temporary Directory with Python2

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.

I should have used backports.tempfile

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!

at the end

In creating this article, I referred to the following pages. Thank you very much.

Recommended Posts

I want to use Temporary Directory with Python2
I want to use MATLAB feval with python
I want to debug with Python
I want to use jar from python
I want to play with aws with python
[Python] I want to use the -h option with argparse
[Python] I want to add a static directory with Flask [I want to use something other than static]
I want to use a wildcard that I want to shell with Python remove
I want to make a game with Python
I want to use ceres solver from python
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to handle optimization with python and cplex
I want to work with a robot in python.
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I want to use the R dataset in python
I want to run a quantum computer with Python
Python: How to use async with
How to use FTP with Python
I want to do ○○ with Pandas
Solution when you want to use cv_bridge with python3 (virtualenv)
I want to be able to analyze data with Python (Part 3)
I want to use R functions easily with ipython notebook
I want to specify another version of Python with pyvenv
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I want to use a virtual environment with jupyter notebook!
[Python] I want to use only index when looping a list with a for statement
I want to tell people who want to import from a higher directory with Python direnv
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere
[Introduction to Python] Let's use foreach with Python
I want to use IPython Qt Console
I wanted to solve ABC172 with Python
I want to know the weather with LINE bot feat.Heroku + Python
I want to monitor UNIQLO + J page updates [Scraping with python]
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to output the beginning of the next month with Python
I want to do a full text search with elasticsearch + python
I want to use an external library with IBM Cloud Functions
I want to use both key and value of Python iterator
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I really want to use GitHub Flavored Markdown (GFM) with Pelican!
I know? Data analysis using Python or things you want to use when you want with numpy
I want to do Dunnett's test in Python
I tried to use lightGBM, xgboost with Boruta
I want to do it with Python lambda Django, but I will stop
[Python] Use JSON with Python
I want to analyze songs with Spotify API 2
I want to tweet on Twitter with Python, but I'm addicted to it
I wanted to solve NOMURA Contest 2020 with Python
I want to memoize including Python keyword arguments