Python tempfile Module

Python provides the tempfile module in order to create temporary files and directories. The tempfile module can be used to create, update, delete or manage temporary files in an easy and managed way. One of the biggest problems with temporary files during application development is they are created explicitly but not deleted automatically after they are not needed or a program execution is complete. By using the tempfile module provided objects like TemporaryFile , NamedTemporaryFile , TemporaryDirectory etc. the temporary files and directories can be easily managed.

Create Temporary File

The TemporaryFile object can be used to create temporary files in an easy way. The temporary file is automatically stored in the operating system temporary file location which is /tmp for Linux and Unix operating systems. The file is opened with w+b in order to write+read in binary mode. First, the tempfile module should be imported with the import statement. The created temporary file is named randomly and when it is closed with the close() method it is deleted automatically.

import tempfile

temp = tempfile.TemporaryFile()
print('Temporary File Content:',temp)
print('Temporary File Name:', temp.name)
temp.close()

Write To Temporary File

The write() method can be used to write into the temporary file. The content should be provided in binary or byte format. The b format specifier can be used easily to convert ASCII to byte as binary.

import tempfile

temp = tempfile.TemporaryFile()
temp.write(b'Hello Pythotect')
temp.close()

Navigate In Temporary File

The seek() method can be used to navigate or seek inside the file. The seek() method accepts the index number of the character or location of the character. For example seek(0) navigates to the start of the file.

import tempfile

temp = tempfile.TemporaryFile()
temp.write(b'Hello Pythotect')
temp.seek(0)
temp.close()

Read From Temporary File

The read() method can be used to read from a temporary file. The cursor location is the start of the read action.

import tempfile

temp = tempfile.TemporaryFile()
temp.write(b'Hello Pythotect')
temp.seek(0)
print(temp.read())
temp.close()

Create Named Temporary File

If the application runs in a multithread way or files are used by multiple processes the NamedTemporaryFile object should be used. It is very easy to pass the temporary file between multiple entities by providing its name.

import tempfile

temp = tempfile.NamedTemporaryFile()
print('Temporary File Content:',temp)
print('Temporary File Name:', temp.name)
temp.close()
/etc/tmp23gts12

Set Prefix or Suffix for Temporary File

The temporary files are named randomly by default. In some cases, we may need to add some prefix or suffix for the name of the temporary file. Prefix and suffix can be used for easy identification with random naming. Other applications can easily identify temporary files with a specific prefix or suffix.

The prefix can be created by using the prefix prarameter. In the following example, we set the prefix as pythontect_ .

import tempfile

temp = tempfile.NamedTemporaryFile(prefix="pythontect_")
print('Temporary File Content:',temp)
print('Temporary File Name:', temp.name)
temp.close()
/tmp/pythontect_12a34rh43

The suffix can be created by using the suffix prarameter. In the following example, we set the suffix as _pythontect .

import tempfile

temp = tempfile.NamedTemporaryFile(suffix="_pythontect")
print('Temporary File Content:',temp)
print('Temporary File Name:', temp.name)
temp.close()

Leave a Comment