Generate temporary files and directories using Python

PythonProgrammingServer Side Programming

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


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
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')
'hello world'
>>> ff.close()


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.close()


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.cleanup()


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')


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")


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()

In this article functions in tempfile module have been explained.

Published on 07-Dec-2018 11:20:32