Monday, November 18, 2024
Google search engine
HomeLanguagesCreate temporary files and directories using Python-tempfile

Create temporary files and directories using Python-tempfile

Tempfile is a Python module used for working with temporary files. 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 default location which is platform dependent. The files created using tempfile are deleted as soon as they are closed. 

Let us see how to create and edit these files:

Creating a Temporary File

The file is created using the TemporaryFile() function. 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 that 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()


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 actually has a visible name this time. 

Adding Suffix and Prefix to a Temporary File

We may choose to add suffix or prefix to the name of a named temporary file, by specifying the parameters ‘suffix‘ and ‘prefix‘. 
 

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

Python3




import tempfile
import os
   
temp_dir = tempfile.TemporaryDirectory()
print(temp_dir)


Output:

<TemporaryDirectory '/tmp/tmpgjl5ki_5'>

Secure Temporary File and Directory

We can create a temporary file in a secure manner 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 ‘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 which the calling user can create files in. 

The list in order of preference is :

  1. The directory named by the TMPDIR environment variable.
  2. The directory named by the TEMP environment variable.
  3. The directory named by the TMP environment variable.
  4. 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.
  5. The current working directory.

Example:

Python3




import tempfile
  
tempfile.tempdir = "/temp"
print(tempfile.gettempdir())


Output:

/temp

RELATED ARTICLES

Most Popular

Recent Comments