Skip to Content
Course content

245: The tempfile Module for Temporary Files

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're working on a complex math problem. You don't want to clutter your final notebook with every single half-finished calculation, failed attempt, and scribbled note you made along the way. Instead, you grab a scratchpad. You use it intensely for twenty minutes, and the moment you have the final answer written in your notebook, you toss the scratchpad in the bin. You don't need to archive it, and you certainly don't want a thousand old scratchpads piling up on your desk.

In Python, the tempfile module is that scratchpad. Sometimes your code needs to store data—maybe a large chunk of a downloaded file or a filtered version of a dataset—but that data is only useful for the duration of the current process. If you manually create files like "temp_data_123.txt", you're inviting trouble. You have to remember to delete them, you have to handle naming collisions if two instances of your script run at once, and you have to worry about where exactly on the disk they should live. tempfile handles all of that housekeeping for you.

The Invisible Scratchpad: TemporaryFile

The most basic tool in this module is TemporaryFile. I like to think of this as a file that exists in a state of quantum uncertainty—it's there while you're using it, but it's virtually invisible to other processes and vanishes the moment you close it.

import tempfile

# Create a temporary file that is deleted as soon as it's closed
with tempfile.TemporaryFile(mode='w+t') as tf:
    tf.write("This is some intermediate data that shouldn't persist.")
    
    # We need to seek back to the beginning to read it
    tf.seek(0)
    print(f"Read from temp: {tf.read()}")

# The file is gone now. No cleanup required.

Notice the mode='w+t'. By default, TemporaryFile opens in binary mode ('w+b'), which is great for images or PDFs, but if you're dealing with text, you'll want to specify the mode. One thing to keep in mind: because these files are often unlinked from the file system immediately after creation (depending on the OS), you can't actually "find" them in your folder explorer while they're open. They are for your program's eyes only.

Giving the Scratchpad a Name

Now, here is where things get practical. I've often run into situations where I'm using a third-party library or an external command-line tool that requires a file path as an argument. An anonymous TemporaryFile won't work there because it doesn't have a path the OS can reference. That's where NamedTemporaryFile comes in.

This creates a file that has a real entry in the file system, meaning it has a .name attribute you can pass around.

import tempfile
import os

with tempfile.NamedTemporaryFile(delete=True) as ntf:
    print(f"The file is physically located at: {ntf.name}")
    ntf.write(b"Passing this path to another tool...")
    ntf.flush() # Ensure data is written to disk before the other tool reads it
    
    # Imagine passing ntf.name to a system call here
    # os.system(f"some_tool {ntf.name}")

# Once the 'with' block ends, the file is deleted automatically.

A quick tip: I always call ntf.flush() if I'm passing the filename to an external process. If you don't, the other tool might try to read the file while the data is still sitting in Python's internal buffer, resulting in an empty file error. It's a classic "gotcha" that has cost me a few hours of debugging in the past.

Managing Temporary Workspaces

Sometimes a single file isn't enough. Maybe you're processing a ZIP archive and need to unpack five different files, manipulate them, and then clear them all out. Creating five separate NamedTemporaryFiles is messy. Instead, use TemporaryDirectory.

This creates a secure, temporary folder. When the context manager exits, it recursively deletes everything inside that folder and the folder itself. It's the ultimate "reset" button for your file system.

import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:
    print(f"Created a temporary workspace at: {tmpdir}")
    
    # Create multiple files inside this directory
    file_path = os.path.join(tmpdir, "working_log.txt")
    with open(file_path, "w") as f:
        f.write("Log entry 1\nLog entry 2")
        
    print(f"Working file created: {file_path}")

# Everything in tmpdir is now nuked from existence.

I generally prefer TemporaryDirectory over managing multiple individual temp files. It keeps your logic clean and ensures that you don't leave a trail of "orphaned" files if your script crashes halfway through a complex operation.




📋 Practical Task

Build a Sensitive Data Sanitizer

You've been tasked with creating a tool that cleans sensitive information (like emails) from a log file before it's uploaded to a cloud storage service. For security reasons, the "dirty" version and the "cleaned" version must never be saved permanently to the disk.

Your Challenge: Write a script that does the following:

  1. Defines a string containing a "dirty" log (include at least three email addresses).
  2. Uses tempfile.NamedTemporaryFile to write the dirty log to a file.
  3. Reads that file back, replaces all email addresses with the string "[REDACTED]" using a regular expression (the re module), and writes the cleaned content to a second temporary file.
  4. Reads the second temporary file and prints the final cleaned output to the console to verify it worked.
  5. Ensures that both temporary files are automatically deleted after the process is complete.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.