Skip to content

#282: Working With Temporary Files

Sometimes we just need a file to store some temporary data and discard it as soon as we are done. In this case, creating permanent files and deleting them manually does not make sense. Fortunately, Python gives us a solution tailored for this situation: the tempfile module. Let us explore how it can help.

Why temporary files?

When we only need files during a program’s execution (or part of it), temporary files help us avoid cluttering the file system and leaking data accidentally. They are useful in situations like:

  • Writing unit tests that require isolated, disposable data
  • Running scripts that generate intermediate files
  • Building web or CLI apps that temporarily handle user-uploaded files
  • Any case where we want the system to clean up automatically

The NamedTemporaryFile method

We can use NamedTemporaryFile to create a temporary file that behaves like a regular file object and deletes itself automatically:

1
2
3
4
5
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as tmp:
    print(f"temp file created at: {tmp.name}")
    tmp.write("Hello, world!")
    tmp.seek(0)
    print(tmp.read())

The with block takes care of the clean-up. As soon as the block ends, Python deletes the file - unless we set delete=False. In that case, we must delete the file manually later.

1
2
3
4
5
tmp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
tmp.write("Attention!")

print(f"File created at: {tmp.name}")
print("\t\t--> delete file by hand")

Make sure you delete the file yourself if you turn off the automatic clean-up!

Temporary directories

When we need a temporary space for multiple files, we can use TemporaryDirectory:

1
2
3
4
5
6
7
with tempfile.TemporaryDirectory() as tmpdirname:
    print(f"Temporary directory created at {tmpdirname}")
    filepath = os.path.join(tmpdirname, "example.txt")
    with open(filepath, 'w') as f:
        f.write("Some temporary content.")
    with open(filepath, 'r') as f:
        print(f.readlines())

Inside the temporary directory, we can create any files we need just like we would in a regular folder. However, once the with block ends, Python deletes the directory and everything inside it automatically - just like it does with NamedTemporaryFile.

What happens after the program ends?

Python’s built-in cleanup feature lets us write self-contained workflows that leave no trace after they run:

  • If we use with tempfile.NamedTemporaryFile(...), Python closes and deletes the file as soon as the block ends.
  • If we use TemporaryDirectory, Python removes the folder and all its contents recursively at the end of the block.
  • If we create temp files outside a with block, we must clean them up ourselves using os.remove() or shutil.rmtree().

Conclusion

The tempfile module gives us a straightforward way to manage temporary files. If we use it properly, Python handles clean-up automatically when our script finishes using those files. This helps us focus on writing our logic while Python keeps the file system tidy - a small but valuable convenience.

Next week we find out how we can play audio files with Python.