Development Python Utilities

Python tempfile module

Often in our programs, we need to store some temporary information about the state of the program and objects which might or might not live beyond the state of the program itself. The data which is saved in these files might not be in the human-readable form or even in a form which can be used by anyone but some programs, algorithms or hackers can find a way to get information out of this data which can sacrifice the security of the system. So, we need to create logic which creates these files, write some data and then delete the files as well. What if there was something which abstracts away so many operations in our program? Seems like there is a module in Python for the same, the tempfile module.

Examples with tempfile module

We will start with simple examples with the Python tempfile module here.

Creating Temporary files

The first thing needed to save temporary data is the files where we can store this data. This can be done using the TemporaryFile() function. The biggest advantage with this function is when a file is created with this function, no links to this file are made in the system’s file system and so, it is not possible for other processes to access these files.

Let’s look at a simple program which makes use of the TemporaryFile() function:

import os
import tempfile

# Using PID in filename for better identification
file = '/tmp/linuxhint_%s.txt' % os.getpid()

# Providing File mode
temp_file = open(file, 'w+b')

try:
  print('temp_file: {0}'.format(temp_file))
  print('temp_file.name: {0}'.format(temp_file.name))
finally:
  temp_file.close()
  # Deleting temporary file ourself
  os.remove(file)

print('TemporaryFile Metadata:')
temp_file = tempfile.TemporaryFile()
try:
  print('temp_file: {0}'.format(temp_file))
  print('temp_file.name: {0}'.format(temp_file.name))
finally:
  # Cleans up the file when close is called
  temp_file.close()

Here is what we get back with this command:

Creating temporary file

Creating temporary file

This file is deleted as soon as the close() function is called on the tempfile reference.

Reading from Temporary file

Even reading from a temporary file is easy and can be done in a single method call in the same module. The advantage with this function is that it helps us to avoid complex IO operations involved if we need to do these operations manually. Here is a program showing this function in action:

import os
import tempfile

tempFile = tempfile.TemporaryFile()
try:
  print('Writing data to tempFile:')
  tempFile.write(b'Any data can go here.')
  tempFile.seek(0)
  print('Reading data form tempFile: \n\t{0}'.format(tempFile.read()))
finally:
  tempFile.close()

Let’s see the output for this command:

Python read from temporary file

Python read from temporary file

All the text in the temporary files was provided back with a single method call.

Writing Plain-text into Temporary File

In our above programs, all data written to files was not in the form of simple plain-text format. If we want to do so for simple text operations, we can just modify the file mode when we open the temporary file for modifications:

import tempfile

fileMode = 'w+t'
with tempfile.TemporaryFile(mode=fileMode) as file:
  file.writelines(['Linux\n', 'Ubuntu\n'])
  file.seek(0)
  for item in file:
    print(item.rstrip())

Here is what we get back with this command:

Writing plain text into the file

Writing plain text into the file

Creating Named Temporary files

The files which need to be spanned across multiple processes must be named so that a process doesn’t delete them when it is completed. Here is how we can create a temporary named file:

import os
import tempfile

tempFile = tempfile.NamedTemporaryFile()
try:
  print('tempFile : {0}'.format(tempFile))
  print('temp.tempFile : {0}'.format(tempFile.name))
finally:
  # Deleting the file as well
  tempFile.close()

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

Let’s see the output for this command:

Named tempfile

Named tempfile

If we don’t delete the file, we can check for its existence in another program and use it if it does exist at the specified location.

Providing File name Suffix and Prefix

To easily identify the files which belongs to our own processes on the file system, we can apply Suffix and Prefix to the file name as well:

import tempfile

tempFile = tempfile.NamedTemporaryFile(suffix='_ubuntu',
prefix='linuxhint_',
dir='/tmp',)
try:
  print('tempFile:', tempFile)
  print('tempFile.name:', tempFile.name)
finally:
  tempFile.close()

Here is what we get back with this command:

Applying prefix and suffix to filename

Applying prefix and suffix to filename

We provided three parameters to the method which acts as Suffix and Prefix for the file name which will be made a the location we specified.

Conclusion

In this lesson, we looked at how we can make use of Python tempfile module to manage temporary files in our code. Read more Python based posts here.

About the author

Shubham Aggarwal

Shubham Aggarwal

I’m a Java EE Engineer with about 4 years of experience in building quality products. I have excellent problem-solving skills in Spring Boot, Hibernate ORM, AWS, Git, Python and I am an emerging Data Scientist.