Introduction

An application uses temporary files to store intermediate information on disk. These files are located in a separate directory, which varies on different operating systems, and the name of these files are unique. The tempfile module provides several functions for creating filesystem resources securely. TemporaryFile() opens and returns an un-named file, NamedTemporaryFile() opens and returns a named file, and mkdtemp() creates a temporary directory and returns its name.

To get the operating system default temporary file directory and file prefix use function as shown below.

import tempfile

# Get information about the temp data environment
print('gettempdir():', tempfile.gettempdir())
print('gettempprefix():', tempfile.gettempprefix())

Creating Temporary File

TemporaryFile() create one temporary file to the default tempfile location. This location may be different between operating systems. File created by TemporaryFile() will be destroyed automatically whenever the file is closed. Also, it does not create any reference to this file in the system’s filesystem table, so it is private to the application. Syntax of this function is

TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)

where

  • mode : Which mode the file has to be open. Defaults value is ‘w+b’ so that the file created can be read and written without being closed.
  • buffering: If the value set to zero (0), no buffering will occur while accessing a file. If the value is set to top one (1), line buffering will be performed while accessing a file.
  • encoding : Decides encoding used to write the data in file.
  • Buffering, encoding, errors and newline are interpreted as for open().

It return a file-like object that can be used as a temporary storage area. The resulting object can be used as a context manager. On completion of the context or destruction of the file object the temporary file will be removed from the filesystem. File created using TemporaryFile() function may or may not have a visible name in the file system.

import tempfile

# Create a temp file using the TemporaryFile class
with tempfile.TemporaryFile(mode="w+t", prefix="Prefix_", suffix="_Suffix") as tfp:

    # Write data to file
    tfp.write('Some text data')

    # Set the file pointer to begining of file
    tfp.seek(0)

    # Print the content of file
    print(tfp.read())

Create Named Temporary File

NamedTemporaryFile() is used to create a file with a visible name in the file system. NamedTemporaryFile() works the same as TemporaryFile(), except that it has a name. Syntax of NamedTemporaryFile() is

NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)

If delete is true (the default), the file is deleted as soon as it is closed.

import tempfile

# Create a temp file using the NamedTemporaryFile class
with tempfile.NamedTemporaryFile(mode="w+t") as tfp:

    # Write data to file
    tfp.write('Some text data')

    # Set the file pointer to begining of file
    tfp.seek(0)

    # Print the content of file
    print(tfp.read())

    print("Name of the file is:", tfp.name)

Creating Temporary Directory

To create a temporary directory, we can use the TemporaryDirectory() function. After all temp files are closed, we need to delete the directory manually. Syntax of this function is

TemporaryDirectory(suffix=None, prefix=None, dir=None)

Directory name can be retrieved from the name attribute of the returned object. The directory can be explicitly cleaned up by calling the cleanup() method.

import os
import tempfile

# Create a temporary directory using the TemporaryDirectory class
with tempfile.TemporaryDirectory() as tdp:

    path = os.path.join(tdp, "tempfile.txt")

    print("Name of the directory is:", tdp)

    tfp = open(path, "w+t")

    # Write data to file
    tfp.write("This is a temp file in temp dir")

    # Set the file pointer to begining of file
    tfp.seek(0)

    # Print the content of file
    print(tfp.read())