How do I create a temporary directory in Python?
In Python 3, TemporaryDirectory from the tempfile module can be used.
import tempfile with tempfile.TemporaryDirectory() as tmpdirname: print('created temporary directory', tmpdirname) # directory and contents have been removed
To manually control when the directory is removed, don’t use a context manager, as in the following example:
import tempfile temp_dir = tempfile.TemporaryDirectory() print(temp_dir.name) # use temp_dir, and when done: temp_dir.cleanup()
The documentation also says:
On completion of the context or destruction of the temporary directory object the newly created temporary directory and all its contents are removed from the filesystem.
At the end of the program, for example, Python will clean up the directory if it wasn’t removed, e.g. by the context manager or the cleanup() method. Python’s unittest may complain of ResourceWarning: Implicitly cleaning up
«Python will clean up the directory if it wasn’t explicitly removed» — I think this only applies if the context manager style ( with . ) is used.
If I run the second example (no context manager) with sleep(5) instead of temp_dir.cleanup() I can ls the temporary directory before the program is done and after it’s finished the same directory is gone. It’s probably best to use a context manager or the cleanup() method, however.
Use the mkdtemp() function from the tempfile module:
import tempfile import shutil dirpath = tempfile.mkdtemp() # . do stuff with dirpath shutil.rmtree(dirpath)
If you use this in a test be sure to remove (shutil.rmtree) the directory because it’s not automatically deleted after use. «The user of mkdtemp() is responsible for deleting the temporary directory and its contents when done with it.» See: docs.python.org/2/library/tempfile.html#tempfile.mkdtemp
In python3, you can do with tempfile.TemporaryDirectory() as dirpath: , and the temporary directory will automatically cleaned up upon exiting the context manager. docs.python.org/3.4/library/…
To expand on another answer, here is a fairly complete example which can cleanup the tmpdir even on exceptions:
import contextlib import os import shutil import tempfile @contextlib.contextmanager def cd(newdir, cleanup=lambda: True): prevdir = os.getcwd() os.chdir(os.path.expanduser(newdir)) try: yield finally: os.chdir(prevdir) cleanup() @contextlib.contextmanager def tempdir(): dirpath = tempfile.mkdtemp() def cleanup(): shutil.rmtree(dirpath) with cd(dirpath, cleanup): yield dirpath def main(): with tempdir() as dirpath: pass # do something here
good alternative to with tempfile.TemporaryDirectory() as tmpdir: on windows since there is this annoying bug that does not let me go into the temporary directory
In python 3.2 and later, there is a useful contextmanager for this in the stdlib https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory
The docs suggest using the TemporaryDirectory context manager, which creates a temporary directory, and automatically removes it when exiting the context manager:
import tempfile with tempfile.TemporaryDirectory() as tmpdirname: print('created temporary directory', tmpdirname) # Outside the context manager, directory and contents have been removed.
Using pathlib to facilitate path manipulation on top of tempfile makes it possible to create new paths with the / path operator of pathlib:
import tempfile from pathlib import Path with tempfile.TemporaryDirectory() as tmpdirname: temp_dir = Path(tmpdirname) file_name = temp_dir / "test.txt" file_name.write_text("bla bla bla") print(temp_dir, temp_dir.exists()) # /tmp/tmp81iox6s2 True print(file_name, "contains", file_name.open().read()) # /tmp/tmp81iox6s2/test.txt contains bla bla bla
Outside the context manager, files have been destroyed
print(temp_dir, temp_dir.exists()) # /tmp/tmp81iox6s2 False print(file_name, file_name.exists()) # /tmp/tmp81iox6s2/test.txt False
Generate temporary file names without creating actual file in Python
Answers to Best way to generate random file names in Python show how to create temporary files in Python. I only need to have a temporary file name in my case.
Calling tempfile.NamedTemporaryFile() returns a file handle after actual file creation. Is there a way to get a filename only? I tried this:
# Trying to get temp file path tf = tempfile.NamedTemporaryFile() temp_file_name = tf.name tf.close() # Here is my real purpose to get the temp_file_name f = gzip.open(temp_file_name ,'wb') .
NamedTemporaryFile guarantees a unique name, (probably) by trying it and retrying if it exists. Getting just a name won’t guarantee that you can actually create the file later, you’re opening to the race condition of someone else using the same name before you.
@Joachim True, there is a race condition here and it would be preferred to avoid that. However, sometimes you have to pass a temporary file name to a function (file open happening internally.) Having a nicely random name gives a much better probability that the race condition will be a non-issue. I think there is a valid need to provide a good temporary filename to minimize the chance of a race condition failure. Of course adding a good prefix and suffix based on the running process and task being performed will provide even less chance of a collision.
@PolyMesh You can avoid the race condition by creating a temporary directory then using a fixed name file within it. So your function accepts a directory, rather than a file, and always creates the same file.
Is it possible to get the path of a tempfile in Python 3
I was wondering if it was possible to get the file path of a temporary file made using the tempfile library. Basically, I’m trying to make a function that intakes some data, and generates a temporary csv file based off of said data. I was wondering if there was a way to get the path of this temporary file?
4 Answers 4
Use tempfile.NamedTemporaryFile to create a temporary file with a name, and then use the .name attribute of the object.
Note that there are platform-specific limitations on how this name can be used. The documentation says:
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
Hmm, what if I wiped the file blank and then re-write it as needed each time? Would it be possible to get a file path to the csv for that?
It doesn’t. As an extension to this question, I was wondering if it was possible to change the name of the NamedTemporaryFile that is created. When I send it out «
Maybe interesting to note that, on Windows, TemporaryFile is NamedTemporaryFile , as can be seen in the source: github.com/python/cpython/blob/3.10/Lib/tempfile.py#L567
tempfile.NamedTemporaryFile has a .dir property which will give you want you want.
EDIT: No, it is not .name , @Barmar, but looking through the source code for tempfile, I don’t see a .dir property either. However, you can use the .name property in conjunction with os.path ‘s dirname method as follows:
with tempfile.NamedTemporaryFile(suffix='.csv', prefix=os.path.basename(__file__)) as tf: tf_directory = os.path.dirname(tf.name)
OP says «I was wondering if there was a way to get the path of this temporary file?» To me, the path is where the given filename is within the filesystem, ergo, the path to the file /home/hd1/IsAnAutisticTwit is /home/hd1/ . os.path.dirname does return this, tf.name returns the absolute path plus the filename delimited by os.sep.
This is the first time I have seen somebody calling the parent path to an absolute path the path to the file. I always thought of «the path to a file» as just the absolute path, was I wrong all these years??
This works just fine to get the full path with the filename
file = tempfile.NamedTemporaryFile() filename = file.name
As one of the more complete answers explained, doing this can have strange results if you don’t close the initial filehandle first and also add the flag to the call to prevent the file from being deleted when the filehandle is closed. As it is, doing anything with the compute name would mean opening and operating on a file that is already open. This is not a good thing to rely on the behavior of.
Cross-platform way of getting temp directory in Python
Is there a cross-platform way of getting the path to the temp directory in Python 2.6? For example, under Linux that would be /tmp , while under XP C:\Documents and settings\[user]\Application settings\Temp .
5 Answers 5
That would be the tempfile module.
It has functions to get the temporary directory, and also has some shortcuts to create temporary files and directories in it, either named or unnamed.
import tempfile print tempfile.gettempdir() # prints the current temporary directory f = tempfile.TemporaryFile() f.write('something on temporaryfile') f.seek(0) # return to beginning of file print f.read() # reads data back from the file f.close() # temporary file is automatically deleted here
For completeness, here’s how it searches for the temporary directory, according to the documentation:
- The directory named by the TMPDIR environment variable.
- The directory named by the TEMP environment variable.
- The directory named by the TMP environment variable.
- A platform-specific location:
- On RiscOS, the directory named by the Wimp$ScrapDir environment variable. * On Windows, the directories C:\TEMP , C:\TMP , \TEMP , and \TMP , in that order. * On all other platforms, the directories /tmp , /var/tmp , and /usr/tmp , in that order. 5. As a last resort, the current working directory.