tempfile.TemporaryDirectory context manager in Python 2.7

2024/11/20 14:24:47

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.
Answer

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:

pip install backports.tempfile

Then use it in your script:

from backports import tempfile
with tempfile.TemporaryDirectory() as temp_dir:# modify files in this dir
# here the temporary directory does not exist any more.
https://en.xdnf.cn/q/26308.html

Related Q&A

Matplotlib returning a plot object

I have a function that wraps pyplot.plt so I can quickly create graphs with oft-used defaults:def plot_signal(time, signal, title=, xlab=, ylab=,line_width=1, alpha=1, color=k,subplots=False, show_grid…

Where is the history file for ipython

I can not determine where the ipython is storing its history.a. There is no ~/.pythonhistory:12:49:00/dashboards $ll ~/.py* ls: /Users/steve/.py*: No such file or directoryb. Nothing special in the pyt…

How do I find what is using memory in a Python process in a production system?

My production system occasionally exhibits a memory leak I have not been able to reproduce in a development environment. Ive used a Python memory profiler (specifically, Heapy) with some success in th…

In Django is there a way to display choices as checkboxes?

In the admin interface and newforms there is the brilliant helper of being able to define choices. You can use code like this:APPROVAL_CHOICES = ((yes, Yes),(no, No),(cancelled, Cancelled), )client_app…

How to get the first 2 letters of a string in Python?

Lets say I have a string str1 = "TN 81 NZ 0025" two = first2(str1) print(two) # -> TNHow do I get the first two letters of this string? I need the first2 function for this.

Python sqlite3 version

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >…

Python debugger (pdb) stopped handlying up/down arrows, shows ^[[A instead

I am using python 2.6 in a virtualenv on an Ubuntu Linux 11.04 (natty) machine. I have this code in my (django) python code:import pdb ; pdb.set_trace()in order to launch the python debugger (pdb).Up u…

Un-persisting all dataframes in (py)spark

I am a spark application with several points where I would like to persist the current state. This is usually after a large step, or caching a state that I would like to use multiple times. It appears …

Can pip (or setuptools, distribute etc...) list the license used by each installed package?

Im trying to audit a Python project with a large number of dependencies and while I can manually look up each projects homepage/license terms, it seems like most OSS packages should already contain the…

Convert DataFrameGroupBy object to DataFrame pandas

I had a dataframe and did a groupby in FIPS and summed the groups that worked fine.kl = ks.groupby(FIPS)kl.aggregate(np.sum)I just want a normal Dataframe back but I have a pandas.core.groupby.DataFram…