Create temporary files and directories using tempfile
Last Updated :
18 Jan, 2024
Python tempfile module allows you to create a temporary file and perform various operations on it. Temporary files may be required when we need to store data temporarily during the program's execution or when we are working with a large amount of data. These files are created with unique names and stored in a platform-dependent default location. The files created using tempfile module are deleted as soon as they are closed.
In this tutorial, we will cover how to create and edit temporary files:
Creating a Temporary File
The file is created using the TemporaryFile() function of the tempfile module. By default, the file is opened in w+b mode, that is, we can both read and write to the open file. Binary mode is used so that files can work with all types of data. This file may not have a proper visible name in the file system.
Example:
Python3
import tempfile
temp = tempfile.TemporaryFile()
print(temp)
print(temp.name)
Output:
<_io.BufferedRandom name=7>
7
The function returns a file-like object that can be used as a temporary storage area. name attribute is used to get the random and unique name of the file.
Note: This is not an actual visible filename and there is no reference to this file in the file system.
Creating a Named Temporary File
The NamedTemporaryFile() function creates a file in the same way as TemporaryFile() but with a visible name in the file system. It takes a delete parameter which we can set as False to prevent the file from being deleted when it is closed.
Example:
Python3
import tempfile
temp = tempfile.NamedTemporaryFile()
print(temp)
print(temp.name)
Output:
<tempfile._TemporaryFileWrapper object at 0x7f77d332f6d8>
/tmp/tmprumbbjz4
This also returns a file-like object as before, the only difference is that the file has a visible name this time.
Adding a Suffix and a Prefix to a Temporary File
We may choose to add a suffix or prefix to the name of a named temporary file, by specifying the parameters 'suffix' and 'prefix'.
Example
Python3
import tempfile
temp = tempfile.NamedTemporaryFile(prefix='pre_', suffix='_suf')
print(temp.name)
Output:
/tmp/pre_ddur6hvr_suf
Reading and Writing to a Temporary File
The write() method is used to write to a temporary file. It takes input as binary data by default. We can pass the string to be written as input, preceded by a 'b' to convert it to binary data.
The write function returns the number of characters written. If we open the file in text mode(w+t), we can use the writelines() method instead, which takes a string parameter. After writing to the file, the pointer is at the end of the file. So, before we can read the contents, seek() method is called to set the file pointer at the start of the file.
seek() takes as argument the index of the character before which we want to place the pointer. The read() function is then used to read the contents.
Example:
Python3
import tempfile
temp = tempfile.TemporaryFile()
temp.write(b'foo bar')
temp.seek(0)
print(temp.read())
temp.close()
Output :
b'foo bar'
Creating a Temporary Directory
Like creating files, we can also create a temporary directory to store our temporary files. The TemporaryDirectory() function is used to create the directory. After we are done working with the temporary files, the directory needs to be deleted manually using os.removedirs().
Example:
Python3
import tempfile
import os
temp_dir = tempfile.TemporaryDirectory()
print(temp_dir)
Output:
<TemporaryDirectory '/tmp/tmpgjl5ki_5'>
Secure Temporary File and Directory
We can securely create a temporary file using mkstemp(). The file created by this method is readable and writable only by the creating user. We can add prefix and suffix parameters like in NamedTemporaryFile(). The default mode is binary, but we can open it in text mode by setting the 'text' parameter as True. This file does not get deleted when closed.
Example:
Python3
import tempfile
secure_temp = tempfile.mkstemp(prefix="pre_",suffix="_suf")
print(secure_temp)
Output:
(71, '/tmp/pre_i5us4u9j_suf')
Similarly, we can create a secure temporary directory using mkdtemp() method.
Example:
Python3
import tempfile
secure_temp_dir = tempfile.mkdtemp(prefix="pre_",suffix="_suf")
print(secure_temp_dir)
Output:
/tmp/pre_9xmtwh4u_suf
Location of Temporary Files
We can set the location where the files are stored by setting the tempdir attribute. The location can be fetched using gettempdir() method. When we create a temporary file or directory, Python searches in a standard list of directories to find one in which the calling user can create files.
The list in order of preference is :
- The directory named by the TMPDIR environment variable.
- The directory named by the TEMP environment variable.
- The directory named by the TMP environment variable.
- A platform-specific directory:
- On Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order.
- On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.
- The current working directory.
Example:
Python3
import tempfile
tempfile.tempdir = "/temp"
print(tempfile.gettempdir())
Output:
/temp
We have covered the methods used to create temporary files and directories. We have also covered different operations like creating named temporary files, adding prefixes and suffixes, and setting the location of the temporary files.
Temporary files are a very important concept in Advanced Python programming. Creating and using temporary files will help you in many operations like handling intermediate data, testing, development, etc.
Similar Reads
Make multiple directories based on a List using Python In this article, we are going to learn how to make directories based on a list using Python. Python has a Python module named os which forms the core part of the Python ecosystem. The os module helps us to work with operating system folders and other related functionalities. Although we can make fol
3 min read
Create an empty file using Python File handling is a very important concept for any programmer. It can be used for creating, deleting, and moving files, or to store application data, user configurations, videos, images, etc. Python too supports file handling and allows users to handle files i.e., to read and write files, along with
3 min read
How to Create Directory If it Does Not Exist using Python? In this article, We will learn how to create a Directory if it Does Not Exist using Python. Method 1: Using os.path.exists() and os.makedirs() methods Under this method, we will use exists() method takes path of demo_folder as an argument and returns true if the directory exists and returns false if
2 min read
Create a directory in Python In Python, you can create directories to store and manage your data efficiently. This capability is particularly useful when building applications that require dynamic file handling, such as web scrapers, data processing scripts, or any application that generates output files.Let's discuss different
3 min read
Change current working directory with Python The OS module in Python is used for interacting with the operating system. This module comes under Python's standard utility module so there is no need to install it externally. All functions in OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments t
2 min read
How to save file with file name from user using Python? Prerequisites: File Handling in PythonReading and Writing to text files in Python Saving a file with the user's custom name can be achieved using python file handling concepts. Python provides inbuilt functions for working with files. The file can be saved with the user preferred name by creating a
5 min read
Python | os.makedirs() method All functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system. In this article, we will see how to create directories recursively using the os module and also about
3 min read
Python tempfile module Tempfile is a Python module used in a situation, where we need to read multiple files, change or access the data in the file, and gives output files based on the result of processed data. Each of the output files produced during the program execution was no longer needed after the program was done.
4 min read
Python | os.mkdir() method All functions in the OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.mkdir() method in Python is used to create a directory in Python or create a directory with Python
4 min read