Piton

Python tempfile module

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

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\t0'.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(['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

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.

How to change Mouse pointer and cursor size, color & scheme on Windows 10
The mouse pointer and cursor in Windows 10 are very important aspects of the operating system. This can be said for other operating systems as well, s...
Motoare de jocuri gratuite și open source pentru dezvoltarea jocurilor Linux
Acest articol va acoperi o listă de motoare de jocuri gratuite și open source care pot fi utilizate pentru dezvoltarea jocurilor 2D și 3D pe Linux. Ex...
Tutorial Shadow of the Tomb Raider pentru Linux
Shadow of the Tomb Raider este a douăsprezecea completare a seriei Tomb Raider - o franciză de jocuri de acțiune-aventură creată de Eidos Montreal. Jo...