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

Kivy Python Tutorial

The importance of mobile software in our world today can never be overemphasized, everyone moves about with their devices...
28/12/2020

Python inotify examples

If you are involved in the world of technology even slightly, you will definitely have heard of the programming language...
29/12/2020

Best Cloud Based IDEs for Python

Development environments are increasingly moving in the cloud in part or full, allowing programmers to access and collaborate...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024