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

Use the mkdtemp() function from the tempfile module:

import tempfile
import shutil

dirpath = tempfile.mkdtemp()
# ... do stuff with dirpath
shutil.rmtree(dirpath)

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.

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

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

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)

Using pathlib to facilitate path manipulation on top of tempfile

In [1]: import tempfile
   ...: from pathlib import Path
   ...: temp_dir = Path(tempfile.TemporaryDirectory().name)
   ...: temp_dir
   ...:
Out[1]: PosixPath('/tmp/tmpqq42526o')

Makes it possible to create new paths using the / path operator of pathlib:

In [2]: temp_dir / "file.txt"
Out[2]: PosixPath('/tmp/tmpqq42526o/file.txt')

Create the directory and write to a file within it (some other file writing method might deal with directory creation automatically)

In [13]: temp_dir.mkdir(parents=True, exist_ok=True)
In [15]: file_name.write_text("bla")

Test whether file and directories exist

In [19]: temp_dir.exists()
Out[19]: True

In [20]: temp_file.exists()
Out[20]: True