How do I create a temporary directory in Python?
Asked Answered
M

6

336

How can I create a temporary directory and get its path in Python?

Mutilate answered 11/7, 2010 at 15:41 Comment(0)
C
348

In Python 3, TemporaryDirectory from the tempfile module can be used.

From the examples:

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 <TemporaryDirectory... if you rely on this, though.

Coco answered 11/3, 2019 at 14:34 Comment(6)
shutil.rmtree(temp_dir.name) is not necessary.Mutant
"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.Spelunker
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.Coco
this is the greatest solution; no dangerous shutil.rmtree requiredMalign
Can you extend the example so that within the with block the cwd is the temp dir?Adipocere
Not sure I understand what you mean. If you want to create the directory in the current working directory, I believe the dir argument can be used. I prefer to keep the example simple and clear, so that people can modify it to their needs, but maybe we can improve it if you show exactly what code you have in mind.Coco
N
286

Use the mkdtemp() function from the tempfile module:

import tempfile
import shutil

dirpath = tempfile.mkdtemp()
# ... do stuff with dirpath
shutil.rmtree(dirpath)
Neoptolemus answered 11/7, 2010 at 15:45 Comment(2)
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.mkdtempPithos
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/…Kimbell
L
46

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
Labarbera answered 22/10, 2015 at 18:41 Comment(1)
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 directorySeymourseys
H
19

The docs suggest using the TemporaryDirectory context manager, which creates a temporary directory, and automatically removes it when exiting the context manager. Using pathlib's slash operator / to manipulate paths, we can create a temporary file inside a temporary directory, then write and read data from the temporary file as follows:

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, the temporary file and the temporary directory have been destroyed

print(file_name, file_name.exists())
# /tmp/tmp81iox6s2/test.txt False

print(temp_dir, temp_dir.exists())
# /tmp/tmp81iox6s2 False
Humorous answered 2/11, 2021 at 11:4 Comment(0)
L
13

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

Lederer answered 22/12, 2016 at 13:3 Comment(0)
U
6

If I get your question correctly, you want to also know the names of the files generated inside the temporary directory? If so, try this:

import os
import tempfile

with tempfile.TemporaryDirectory() as tmp_dir:
    # generate some random files in it
     files_in_dir = os.listdir(tmp_dir)
Ushaushant answered 30/12, 2019 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.