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 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:
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 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:
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:
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:
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 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:
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:
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:
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.