Create Temp File in Python

Sometimes applications require storage and processing of temporary data. For such purposes, Python provides the tempfile library that can be used to create such files and directories.

How to Create Temp File in Python

This tutorial will demonstrate how to create temp file in Python.

Using the tempfile.NamedTemporaryFile object

The NamedTemporaryFile constructor is used to create objects that resemble a file-like object in the temporary storage area and can be used to read and write data. The file is created securely using some predefined rules and is destroyed as soon as it is closed. The file has a certain name in the system that can be retrieved with the name attribute.

We can open the file in the required mode using the mode parameter. Other parameters are also available like encoding, prefix, newline, and more.

The default value of the mode parameter is w+b which means we can read and write using the same object without closing it.

See the code below.

Output:

Directory: C:\Users\AppData\Local\Temp\tmp76uo8lzt Content: b’Java2Blog’

In the above example, we create Temp file in Python and write data to the same. The name attribute is used to access the directory and name of the temporary file. The write() function is used to write data to this file.

Then we seek the file pointer to the starting position using the seek() method, read the data using the read() function, and display the same. Since we are using the with statement there is no need to use the close() function to destroy the object.

Using the tempfile.TemporaryFile object

The TemporaryFile object works similarly to the NamedTemporaryFile object but it does not explicitly return a file name. However, for platforms that nor PSOIX neither Cygwin, this is an alias for NamedTemporaryFile object.

We use it similarly to the previous function.

Output:

Content: b’Java2Blog’

Using the mkstemp() function

This function works similarly to the previous methods to create temp file in Python. It returns a tuple with the file object and the file name.

However, it does not provide functions to read or write data but we can use the methods from the os library. Also unlike the previous methods, we need to destroy the file explicitly after creating it.

See the code below.

Output:

C:\Users\AppData\Local\Temp\tmpueokqk3y

Conclusion

To conclude this article, we discussed several methods to create temp file in Python. For this, we used the tempfile library that allows us to create temporary files and directories. T

TemporaryFile and NamedTemporaryFile objects work very similarly in allowing us to create file-like objects that create such temporary files and we can read-write data accordingly. The difference between the two lies in the fact that the latter returns an explicit name of the file that can be accessed using the name attribute.

We can also use the mkstemp() function that works similarly to these methods but we need to explicitly close and clean the temporary files.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *