Skip to content

[Python] Creating and Auto-Removing Temporary Directories with Python's `tempfile`

Last Updated on 2024-01-07 by Clay

Introduction

Today I was reading the training source code of DreamBooth, and I found the tempfile built-in module; I happened to reconstruct a script for model's layer merging, suddenly thought that the module would make the code more elegant, so I made this note.

Simply put, tempfile is a Python built-in module and it can create a temporary directory and put some data on the disk.

So when we operate this module, it is actually reading and writing on the hard drive. What is helpful is that after our workflow ends, this temporary directory will be automatically deleted without us having to worry about deleting it.

This module is ideal for handling data that requires temporary storage on disk without the need for long-term retention.


Usage

import tempfile
import os

# Create
with tempfile.TemporaryDirectory() as tmpdirname:
    # And you can save a file under the directory
    tmpfilepath = os.path.join(tmpdirname, "tempfile.txt")
    print(f"Create temporary directory: {tmpdirname}")

    with open(tmpfilepath, 'w') as f:
        f.write("testing")


Output:

Create temporary directory: /tmp/tmpi824kljq/tmpfile.txt


And then, if the program ends, you cannot find this temporary directory.


References


Read More

Tags:

Leave a Reply