You are currently viewing How To Use tempfile To Create Temporary Files and Directories in Python

How To Use tempfile To Create Temporary Files and Directories in Python

Python has a rich collection of standard libraries to carry out various tasks. In Python, there is a module called tempfile that allows us to create and manipulate temporary files and directories.

We can use tempfile to create temporary files and directories for storing temporary data during the program execution. The module has functions that allow us to create, read, and manipulate temporary files and directories.

In this article, we’ll go over the fundamentals of the tempfile module, including creating, reading, and manipulating temporary files and directories, as well as cleaning up after we’re done with them.

Creating a temporary file using tempfile

The tempfile module includes a function called TemporaryFile() that allows us to create a temporary file for use as temporary storage. Simply enter the following code to create a temporary file.

First, we imported the module, and because it is part of the standard Python library, we did not need to install it.

Then we used the TemporaryFile() function to create a temporary file, which we saved in the temp_file variable. Then we added print statements to display the tempfile object and temporary file name.

Finally, we used the close() function to close the file, just as we would any other file. This will automatically clean up and delete the file.

Output

We got the location of the object in memory when we printed the object temp_file, and we got the random name tmp5jyjavv2 with the entire path when we printed the name of the temporary file.

We can specify dir parameter to create a temporary file in the specified directory.

The file will be created in the current working directory.

Writing text and reading it

The TemporaryFile() function sets the mode parameter 'w+b' to the default value to read and write files in a binary mode. We can also use 'w+t' mode to write text data into the temporary file.

In the above code, we created a temporary file and then used the write() function to write data into it, passing the string in bytes format (as indicated by the prefix 'b' before the content).

Then we used the read() function to read the content, but first, we specified the position from which to begin reading the content using the seek(0) (start from the beginning) function, and finally, we closed the file.

Using context manager

We can also create temporary files using the context managers such as with keyword. Also, the following example will show us how to write and read text data into the temporary file.

In the above code, we used the with statement to create a temporary file and then opened it in text mode('w+t') and did everything the same as we do for handling other files in Python.

Named temporary file

To take more control over making the temporary file like naming the temporary file as we want or keeping it or deleting it, then we can use NamedTemporaryFile().

When creating a temporary file, we can specify a suffix and a prefix, and we can choose whether to delete or keep the temporary file. It has a delete parameter that defaults to True, which means that the file will be deleted automatically when we close it, but we can change this value to False to keep it.

The output of the above code is the same as when we created the temporary file using the TemporaryFile() function, it’s because NamedTemporaryFile() operates exactly as TemporaryFile() but the temporary file created using the NamedTemporaryFile() is guaranteed to have a visible name in the file system.

How to customize the name of a temporary file?

As you can see, the name of our temporary file is generated at random, but we can change that with the help of the prefix and suffix parameters.

The temporary file will now be created when the code is run, and its name will be prefixed and suffixed with the values we specified in the code above.

Creating temporary directory

To create a temporary directory, we can use TemporaryDirectory() function from the tempfile module.

The above code will create a temporary directory inside the TempDir directory that we created in the root directory. Then we printed the name of the temporary directory and then called the cleanup() method to explicitly clean the temporary directory.

We can use with statement to create a temporary directory and we can also specify suffix and prefix parameters.

When we create a temporary directory using the with statement the name of the directory will be assigned to the target of the as clause. To get the name of the temporary directory, we passed tempdir rather than tempdir.name in the print statement.

You will notice that this time we didn’t use the cleanup() method, that’s because the temporary directory and its content are removed after the completion of the context of the temporary directory object.

SpooledTemporaryFile

The SpooledTemporaryFile() function is identical to TemporaryFile(), with the exception that it stores the data in memory up until the maximum size is reached or fileno() method is called.

With the max_size parameter set to 10, we used the SpooledTemporaryFile() function to create a spooled temporary file. It indicates that file size up to 10 can be stored in the memory before it is rolled over to the on-disk file.

Then we wrote some data into the file and printed the size of the file using the __sizeof__ attribute.

There is a rollover() method by which we can see whether the data is in the memory or rolled over to the on-disk temporary file. This method returns a boolean value, True means the data is rolled over and False means the data is still in the memory of the spooled temporary file.

The temporary file object is SpooledTemporaryFile, as can be seen. Because the amount of data we wrote into the file exceeded the allowed size, the data was rolled over, and we received the boolean value True.

Depending on the mode we specified, this function returns a file-like object whose _file attribute is either an io.BytesIO or an io.TextIOWrapper (binary or text). Up until the data exceeded the max size, this function holds the data in memory using the io.BytesIO or io.TextIOWrapper buffer.

Output

Until the data is rolled over, we ran a while loop and printed the _file attribute of the SpooledTemporaryFile object. As we can see the data was stored in the memory using the io.TextIOWrapper buffer(mode was set to text) and when the data was rolled over, the code returned the temporary file object.

We can do the same with data in binary format.

Output

We can also call fileno() to roll over the file content.

Output

Low-level functions

There are some low-level functions included in the tempfile module. We’ll explore them one by one.

mkstemp and mkdtemp

mkstemp is used for creating a temporary file in the most secure manner possible and mkdtemp is used to create a temporary directory in the most secure manner possible.

They also have parameters like suffixprefix and dir but mkstemp has one additional text parameter which defaults to False(binary mode), if we make it True then the file will be opened in text mode.

Note: There is a function called mktemp() also which is deprecated.

gettempdir and gettempdirb

gettempdir() is used to return the name of the directory used for temporary files and gettempdirb() also return the name of the directory but in bytes.

Conclusion

The tempfile module, which offers functions to create temporary files and directories, was covered in this article. We have used various functions from the tempfile module in code examples, which has helped us better understand how these functions operate.


🏆Other articles you might be interested in if you liked this one

Use match case statement for pattern matching in Python.

Types of class inheritance in Python with examples.

An improved and modern way of string formatting in Python.

Integrate PostgreSQL database with Python.

Argmax function in NumPy and TensorFlow – Are they the same?

Take multiple inputs from the user in a single line in Python.

Perform a parallel iteration over multiple iterables using zip() function in Python.


That’s all for now

Keep Coding✌✌