Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python HowTo – Using the tempfile Module in Python

Python Tempfile

Hello everyone! In today’s article, we’ll be looking at how we can use the tempfile module in Python.

This module is very useful when you want to store temporary files. There may be a need to store temporary data from an application point of view, so these files can prove to be very useful!

Python provides us with the tempfile module, which gives us an easy to use interface. Let’s get started.


The tempfile module in Python

This module is a part of the standard library (Python 3.x), so you don’t need to install anything using pip. You can simply import it!

import tempfile

We will look at how we can create temporary files and directories now.

Creating Temporary Files and Directories

The tempfile module gives us the TemporaryFile() method, which will create a temporary file.

Since the file is temporary, other programs cannot access this file directly.

As a general safety measure, Python will automatically delete any temporary files created after it is closed. Even if it remains open, after our program completes, this temporary data will be deleted.

Let’s look at a simple example now.

import tempfile

# We create a temporary file using tempfile.TemporaryFile()
temp = tempfile.TemporaryFile()

# Temporary files are stored here
temp_dir = tempfile.gettempdir()

print(f"Temporary files are stored at: {temp_dir}")

print(f"Created a tempfile object: {temp}")
print(f"The name of the temp file is: {temp.name}")

# This will clean up the file and delete it automatically
temp.close()

Output

Temporary files are stored at: /tmp
Created a tempfile object: <_io.BufferedRandom name=3>
The name of the temp file is: 3

Let’s now try to find this file, using tempfile.gettempdir() to get the directory where all the temp files are stored.

After running the program, if you go to temp_dir (which is /tmp in my case – Linux), you can see that the newly created file 3 is not there.

ls: cannot access '3': No such file or directory

This proves that Python automatically deletes these temporary files after they are closed.

Now, similar to creating temporary files, we can also create temporary directories using the tempfile.TemporaryDirectory() function.

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

The directory names are random, so you can specify an optional suffix and/or prefix to identify them as part of your program.

Again, to ensure safe deletion of the directory after the relevant code completes, we can use a context manager to securely wrap this!

import tempfile

with tempfile.TemporaryDirectory() as tmpdir:
    # The context manager will automatically delete this directory after this section
    print(f"Created a temporary directory: {tmpdir}")

print("The temporary directory is deleted")

Output

Created a temporary directory: /tmp/tmpa3udfwu6
The temporary directory is deleted

Again, to verify this, you can try to go to the relevant directory path, which won’t exist!

1. Reading and Writing from a Temporary File

Similar to reading or writing from a file, we can use the same kind of function calls to do this from a temporary file too!

import tempfile

with tempfile.TemporaryFile() as fp:
    name = fp.name
    fp.write(b'Hello from AskPython!') # Write a byte string using fp.write()
    fp.seek(0) # Go to the start of the file
    content = fp.read() # Read the contents using fp.read()
    print(f"Content of file {name}: {content}")

print("File is now deleted")

Let’s now look at the output.

Output

Content of file 3: b'Hello from AskPython!'
File is now deleted

Indeed, we were able to easily read and write from/to temporary files too.

2. Creating Named Temporary Files

In some situations, named temporary files may be useful to make the files visible to other scripts/processes so that they can access it, while it is not yet closed.

The tempfile.NamedTemporaryFile() is useful for this. This has the same syntax as creating a normal temporary file.

import tempfile

# We create a named temporary file using tempfile.NamedTemporaryFile()
temp = tempfile.NamedTemporaryFile(suffix='_temp', prefix='askpython_')

print(f"Created a Named Temporary File {temp.name}")

temp.close()

print("File is deleted")

Output

Created a Named Temporary File /tmp/askpython_r2m23q4x_temp
File is deleted

Here, a named temporary file with a prefix of askpython_ and suffix of _temp is created. Again, it will be deleted automatically after it is closed.


Conclusion

In this article, we learned how we can use the tempfile module in Python to deal with temporary files and directories.

References