Python tempfile module

Filed Under: Python

Python Tempfile

In every programming languages, it’s often that program need to store temporary data into the file system by creating temporary directories and files. This data might not be completely ready for output but still, accessing this data can sacrifice the security layer of a program. Writing complete code for just managing temporary data is a cumbersome process as well. This is because we need to write logic about creating random names for these files and directories and then writing to them followed by deleting the data once all operations are complete.

All of these steps are made very easy with tempfile module in Python. The tempfile module provides easy functions through which we can make temporary files and directories and access them easily as well. Let’s see this module in action here.

Creating Temporary files

When we need to create a temporary file to store data, TemporaryFile() function is something we want. The advantage this function provides us is that when it makes a new file, there are no references to the file in the platform’s file system and so, it is impossible for other programs to access those files.

Here is a sample program which creates a temporary file and cleans up the file when TemporaryFile is closed:


import os
import tempfile

# Using PID in filename
filename = '/tmp/journaldev.%s.txt' % os.getpid()

# File mode is read & write
temp = open(filename, 'w+b')

try:
    print('temp: {0}'.format(temp))
    print('temp.name:  {0}'.format(temp.name))
finally:
    temp.close()
    # Clean up the temporary file yourself
    os.remove(filename)

print('TemporaryFile:')
temp = tempfile.TemporaryFile()
try:
    print('temp: {0}'.format(temp))
    print('temp.name: {0}'.format(temp.name))
finally:
    # Automatically cleans up the file
    temp.close()

Let’s see the output for this program:

python tempfile example

Creating Temporary file

In the first code snippet, we clean the file ourself. In the next code snippet, as soon as the TemporaryFile is closed, the file is completely deleted from the system as well.

Once you run this program, you will see that no files exist in the filesystem of your machine.

Reading from Temporary file

Luckily, reading all data from a temporary file is just a matter of single function call. With this, we can read data we write in the file without handling it byte by byte or doing complex IO operations.

Let’s look at a sample code snippet to demonstrate this:


import os
import tempfile

temp_file = tempfile.TemporaryFile()
try:
    print('Writing data:')
    temp_file.write(b'This is temporary data.')
    temp_file.seek(0)   
    print('Reading data: \n\t{0}'.format(temp_file.read()))
finally:
    temp_file.close()

Let’s see the output for this program:

Reading a Tempfile in python

Reading a Tempfile

We only had to call read() function on the TemporaryFile() object and we were able to get all data back from the temporary file. Finally, note that we wrote only byte data to this file. In the next example, we will write plain-text data to it.

Writing Plain-text into Temporary File

With slight modifications in the last program we write, we can write simple text data into a temporary file as well:


import tempfile

file_mode = 'w+t'
with tempfile.TemporaryFile(mode=file_mode) as file:
    file.writelines(['Java\n', 'Python\n'])
    file.seek(0)
    for line in file:
        print(line.rstrip())

Let’s see the output for this program:

Writing Plain text into Temporary file

Writing Plain text into Temporary file


We changed the mode of the file to t, which is important if we want to write textual data into our temporary files.

Creating Named Temporary files

Named Temporary files are important to have because there might be scripts and applications which spans across multiple processes or even machines. If we name a temporary file, it is easy to pass it between the parts of the application.

Let’s look at a code snippet which makes use of NamedTemporaryFile() function to create a Named Temporary file:


import os
import tempfile

temp_file = tempfile.NamedTemporaryFile()
try:
    print('temp_file : {0}'.format(temp_file))
    print('temp.temp_file : {0}'.format(temp_file.name))
finally:
    # Automatically deletes the file
    temp_file.close()

print('Does exists? : {0}'.format(os.path.exists(temp_file.name)))

Let’s see the output for this program:

Creating named temporary file

Creating named temporary file

Providing File name Suffix and Prefix

Sometimes, we need to have some prefix and suffix in the file names to identify a purpose that file fulfils. This way, we can create multiple files so that they are easy to identify in a bunch of files about what file achieves a specific purpose.

Here is a sample program which provides prefix and suffix into the file names:


import tempfile

temp_file = tempfile.NamedTemporaryFile(suffix='_temp', 
                                   prefix='jd_', 
                                   dir='/tmp',)
try:
    print('temp:', temp_file)
    print('temp.name:', temp_file.name)
finally:
    temp_file.close()

Let’s see the output for this program:

Tempfile with Prefix and Suffix

Tempfile with Prefix and Suffix


We just provided three arguments to the NamedTemporaryFile() function and it manages providing prefix and suffix to the temporary file this module creates.

Conclusion

In this lesson, we studied how we can securely create temporary files for our programs and application. We also saw how we can create files which can span across multiple processes and saw how we can provide a prefix & suffix to fle names so that they easily signify what data they contain.

Read more Python posts here.

Download the Source Code

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages