Is there a way to create a temporary directory in a context manager with Python 2.7?
with tempfile.TemporaryDirectory() as temp_dir:
# modify files in this dir
# here the temporary diretory does not exist any more.
Is there a way to create a temporary directory in a context manager with Python 2.7?
with tempfile.TemporaryDirectory() as temp_dir:
# modify files in this dir
# here the temporary diretory does not exist any more.
Another option is the "backports.tempfile" package on pypi: https://pypi.python.org/pypi/backports.tempfile
Quoting the project's description: "This package provides backports of new features in Python’s tempfile module under the backports namespace."
Install with:
Then use it in your script:
tempfile.TemporaryDirectory()
was added to the tempfile standard library in Python 3.2It's a simple wrapper for
tempfile.mkdtemp
. It is coded in pure Python and can be easily ported to Python 2.7.For example: