Generate temporary files and directories using Python


The tempfile module in standard library defines functions for creating temporary files and directories. They are created in special temp directories that are defined by operating system file systems. For example, under Windows the temp folder resides in profile/AppData/Local/Temp while in linux the temporary files are held in /tmp directory.

Following functions are defined in tempfile module

TemporaryFile()

This function creates a temporary file in the temp directory and returns a file object, similar to built-in open() function. The file is opened in wb+ mode by default, which means it can be simultaneously used to read/write binary data in it. What is important, the file’s entry in temp folder is removed as soon as the file object is closed. Following code shows usage of TemporaryFile() function.

>>> import tempfile
>>> f = tempfile.TemporaryFile()
>>> f.write(b'Welcome to TutorialsPoint')
>>> import os
>>> f.seek(os.SEEK_SET)
>>> f.read()
b'Welcome to TutorialsPoint'
>>> f.close()

Following example opens TemporaryFile object in w+ mode to write and then read text data instead of binary data.

>>> ff = tempfile.TemporaryFile(mode = 'w+')
>>> ff.write('hello world')
>>> ff.seek(0)
>>> ff.read()
'hello world'
>>> ff.close()

NamedTemporaryFile()

This function is similar to TemporaryFile() function. The only difference is that a file with a random filename is visible in the designated temp folder of operating system. The name can be retrieved by name attribute of file object. This file too is deleted immediately upon closing it.

>>> fo = tempfile.NamedTemporaryFile()
>>> fo.name
'C:\Users\acer\AppData\Local\Temp\tmpipreok8q'
>>> fo.close()

TemporaryDirectory()

This function creates a temporary directory. You can choose the location of this temporary directory by mentioning dir parameter. Following statement will create a temporary directory in C:\python36 folder.

>>> f = tempfile.TemporaryDirectory(dir = "C:/python36")
<TemporaryDirectory 'C:/python36\ tmp9wrjtxc_'>

The created directory appears in the dir1 folder. It is removed by calling cleanup() function on directory object.

>>> f.name
'C:/python36\tmp9wrjtxc_'
>>> f.cleanup()

mkstemp()

This unction also creates a temporary file, similar to TemporaryFile() function. Additionally, suffix and prefix parameters are available to add with temporary file created. Unlike in case of TemporaryFile(), the created file is not automatically removed. It should be removed manually.

>>> f = tempfile.mkstemp(suffix = '.tp')
C:\Users\acer\AppData\Local\Temp\tmpbljk6ku8.tp

mkdtemp()

This function also creates a temporary directory in operating system’s temp folder and returns its absolute path name. To explicitly define location of its creation, use dir parameter. This folder too is not automatically removed.

>>> tempfile.mkdtemp(dir = "c:/python36")
'c:/python36\tmpruxmm66u'

gettempdir()

This function returns name of directory to store temporary files. This name is generally obtained from tempdir environment variable. On Windows platform, it is generally either user/AppData/Local/Temp or windowsdir/temp or systemdrive/temp. On linux it normally is /tmp. This directory is used as default value of dir parameter.

>>> tempfile.gettempdir()
'C:\Users\acer\AppData\Local\Temp'

In this article functions in tempfile module have been explained.

Updated on: 25-Jun-2020

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements