Proper use of Python temporary files

  • 2021-10-15 10:59:29
  • OfStack

Catalogue 1, Preface
2. Introduction to the tempfile module
3. Example introduction
3.1 Incorrect example:
3.2 Correct Examples

1. Preface

Temporary files are usually used to hold data that cannot be saved in memory or to pass on to external programs that must be read from the file. We will generate only 1 filename in the/tmp directory, but creating temporary files safely is not so simple, and there are many rules to follow. Never try to do it yourself, but do it with the help of library functions. But also carefully clean up temporary files.

The biggest problem caused by temporary files is that they can predict file names, which leads malicious users to predict temporary file names, thus creating soft links to hijack temporary files.

2. Introduction to the tempfile module

The module used to create temporary file 1 is tempfile, and the following are commonly used in this module library function:

tempfile. mktemp # is not safe and prohibited
tempfile. mkstemp # Randomly creates tmp files. By default, the files created are in the/tmp directory. Of course, you can also specify (you can use)
tempfile. TemporaryFile # Creates files in memory, files are not stored on disk, and are deleted after closing (available)
tempfile. NamedTemporaryFile (delete = True) When delete = True, it works as above 1. When it is False, it will be stored on disk (can be used)

3. Example introduction

The following methods introduce the safe and unsafe ways to create temporary files respectively.

3.1 Incorrect example:

Incorrect 1:


import os
import tempfile
 
# This will most certainly put you at risk
tmp = os.path.join(tempfile.gettempdir(), filename)
if not os.path.exists(tmp):
 with open(tmp, "w") file:
  file.write("defaults")

Incorrect 2:


import os
import tempfile
 
open(tempfile.mktemp(), "w")

Incorrect 3:


filename = "{}/{}.tmp".format(tempfile.gettempdir(), os.getpid())
open(filename, "w")

3.2 Correct Examples

Correct 1:


fd, path = tempfile.mkstemp()
try:
 with os.fdopen(fd, 'w') as tmp:
  # do stuff with temp file
  tmp.write('stuff')
finally:
 os.remove(path)

Correct 2:


#  When the handle is closed, the file is deleted 
with tempfile.TemporaryFile() as tmp:
 # Do stuff with tmp
 tmp.write('stuff')

Correct 3:


tmp = tempfile.NamedTemporaryFile(delete=True)
try:
 # do stuff with temp
 tmp.write('stuff')
finally:
 tmp.close() #  Close and delete files 

The above is the correct use of Python temporary files in detail, more information about the use of Python temporary files please pay attention to other related articles on this site!


Related articles: