Python tempfile module

28/12/2020
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

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: nt{0}’.format(tempFile.read()))
finally:
  tempFile.close()

Let’s see the output for this command:

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([‘Linuxn’, ‘Ubuntun’])
  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

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

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

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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Python Plotly Tutorial

Plotly is an analytics company known for developing real-time analytics, statistics and graphing tools for web-based application...
29/12/2020

Python datetime module

Working with date and time objects is one of the most common tasks I have done in the Python utility scripts I write for...
28/12/2020

How to Calculate Matrices in Python Without NumPy

For many applications, you need mathematics. In Python there is the maths module which handles the basics such as rounding,...
29/12/2020