SoFunction
Updated on 2024-11-12

Python Runs Code Easily and Without Trace with the tempfile Package

I. Introduction

Here is an introduction to the use of temporary files and folders in python.

The tempfile package is used:

pip install tempfile

/3/library/

II. Temporary folders

2.1 Getting Temporary Folders

# Get the temporary folder
tmpdir = ()
print(tmpdir) #/tmp

2.2 Generating temporary folders

# Option 1: Generate a default temporary folder
tmpdir = ()
print(tmpdir) #/tmp/tmpui77cgud

# Mode 2: Generate a customized temporary folder (specify prefix, suffix, directory, you can specify some of them), suffix: suffix, prefix: prefix, dir: directory.

tmpdir = (suffix='_txt', prefix='tp_dir_', dir='/home/tmp/py_rs_file')

print(tmpdir) # /home/tmp/py_rs_file/tp_dir_06l_o2dm_txt

III. Provisional documentation

3.1 Generation of temporary files that are not automatically deleted (on closure)

# Mode 1: Generate a default temporary file, which is a binary file by default.

tmpfile = ()[1]
print(tempfile) #/tmp/tmp75kazf_8
# Data writing
with open(tmpfile, 'w+') as t_f:
    t_f.writelines('hello world')

# Mode 2: Generate a customized temporary file (specify prefix, suffix, directory, file type parameters, you can specify some of them), suffix: suffix, prefix: prefix, dir: directory, text: file type, True is text, false is binary.

tmpfile = (suffix='.txt', prefix='tp_', dir='/home/tmp/py_rs_file', text=True)[1]
print(tempfile) # /home/tmp/py_rs_file/tp_pn2973g0.txt

# Data writing
with open(tmpfile, 'w+') as t_f:
    t_f.writelines('hello world')

3.2 Generation of automatically deleted temporary files

# Mode 1: Create a temporary file that is automatically deleted when the file is closed
tmpfile = (mode='w+t')
('hello world') ## Data writing
(0)
tmpTxt = () #Data reading
print(tmpTxt)
() # Files are automatically deleted on shutdown

# Mode 2: Create a temporary file and determine if it is automatically deleted when the file is closed according to the delete parameter, True: delete False: do not delete
with (delete=False) as tmpfile:
    file_name = 
    print(file_name) #/tmp/tmp73zl8gmn
    ('hello world'.encode())
    (0)
    tmpTxt = ().decode()
    print(tmpTxt)

# Option 3: Create a customized temporary file, which can be automatically deleted or not according to the delete parameter when the file is closed, True: delete False: do not delete
# Other configuration parameters are, mode: file mode (w+b for binary mode (default), w+t for text mode), suffix: suffix, prefix: prefix, dir: directory
with (mode='w+t', suffix='.txt', prefix='tp_', dir='/home/tmp/py_rs_file',delete=False) as tmpfile:
    file_name = 
    print(file_name) #/home/tmp/py_rs_file/tp_fcwpmh3l.txt
    ('hello world')
    (0)
    tmpTxt = ()
    print(tmpTxt)

Depending on the situation, temporary resources can call directly on memory or database storage.

technical exchange

Feel free to republish, bookmark, and like something to support it!

在这里插入图片描述

to this article on Python use tempfile package to easily run the code without trace of the article is introduced to this, more related Python tempfile package content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!