In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the tempfile module of Python". In the daily operation, I believe many people have doubts about how to use the tempfile module of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the tempfile module of Python". Next, please follow the editor to study!
1. Brief introduction of tempfile module 1. Application scenario of tempfile module
Python's tempfile module is a cross-platform tool for creating temporary files or folders. In large-scale data processing projects, some processing results do not need to be shown to users, but their applications are throughout the project. In this case, we need to use tempfile module to solve this problem.
2. The method of tempfile module
The tempfile module mainly includes three types of functions:
(1) four high-level interfaces: TemporaryFile, NamedTemporaryFile, SpooledTemporaryFile and TemporaryDirectory, which provide automatic cleanup and can be used as context managers.
Function name description TemporaryFile and NamedTemporaryFile advanced temporary file object creation function SpooledTemporaryFile spooling mode advanced temporary file object creation function TemporaryDirectory advanced temporary directory creation function
(2) in addition, two underlying functions, mkstemp () and mkdtemp (), are used to generate temporary underlying files and folders, which need to be cleared manually when they are finished.
Function name describes mkstemp () underlying temporary file creation function mkdtemp () underlying temporary directory creation function
(3) finally, the tempfile module also has some operation functions gettempdir (), gettempdirb (), gettempprefix, gettempprefixb for the attributes of files and folders.
Function name description gettempdir () returns temporary folder name in text format function gettempdirb () returns temporary folder name function gettempprefix returns temporary file name prefix gettempprefixb in binary format
All user callable functions and constructors of tempfile take additional parameters that enable the management of temporary file directories and locations. This module securely creates temporary files in the shared temporary directory and gives the temporary text a random name.
Second, the introduction of the main functions of tempfile module
Advanced temporary file object creation functions: Temporaryfile and NamedTemporaryFile
1. Temporaryfile function
The Temporaryfile function returns a class file object as a temporary store, which rebuilds the file schema securely using the same rules as the mkstemp () function, and is destroyed as soon as it is closed (including implicit closure for garbage collection objects). It is important to note that in the Unix system environment, the directory of the file is either not created at all or deleted immediately after the file is created. That is, the code does not rely on temporary files created by this function, including their names, which is the difference between this function and the NamedTemporaryfile function.
The calling format of the TemporaryFile function is:
TemporaryFile (mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, *, error=None)
Function argument options:
(1) the mode parameter: the default is wrapr, so that the file can be read and written when it is created.
(2) buffering, encoding, errors and newline parameters: used to explain the behavior of the open () function.
(3) dir, prefix, and suffix parameters: have the same meaning and default settings as mkstemp ().
Here is a typical example:
Create a temporary file under import tempfile# and write some data fp = tempfile.TemporaryFile () fp.write (bb'Hello fp.seek') # read data fp.seek (0); fp.read ()
In addition, the object generated by this function can be used as a context manager (see example). After completing context management or destroying file objects, temporary files are deleted from the file system.
Import tempfilewith tempfile.TemporaryFile () as fp: fp.write (b'Hello Xiaoliangzhi') Fp.seek (0) fp.read () 2, NamedTemporaryfile function
Except that the NamedTemporaryfile () function implements only one visible name in the file system, its function is exactly the same as the TemporaryFile () function. After executing this function, we can retrieve the file name from the name property of the returned class file object. On Unix systems, you can use that name to open the resulting file while the named temporary file is open.
The calling format of the NamedTemporaryFile function is very similar to that of the TemporaryFile function, except that it has a delete parameter:
NamedTemporaryFile (mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=Nonde, delete=True, *, error=None)
Delete parameter option: if this parameter is True (default), the file will be deleted as soon as it is closed. The returned object is always a class file object, and its file properties are the underlying real file object. That is, this class file object can be used in with context management, just like normal files.
3. Spooling mode advanced temporary file object creation function: SpooledTemporaryfile
Brief introduction of SPOOL Technology:
SPOOL is Simultaneous Peripheral Operation On-Line.
An abbreviation for the online parallel operation of external devices, which is a technology about how slow character devices exchange information with computer hosts, often referred to as "spooling technology". In fact, strictly speaking, SPOOL system is not only different from offline mode, but also different from direct coupling mode. SPOOL technology is actually a kind of peripheral equipment online operation technology, also known as queued dump technology. It increases the queue dump link of "input well" and "output well" between input and output, in order to eliminate the "online" waiting time of users.
The SpooledTemporaryFile () function is exactly the same as TemporaryFile () except that the data is spooled in memory. Until the file size exceeds max_size or until the file's fileno () method is called, the contents are written to disk using the same operation as TemporaryFile ().
This function also returns a class file object whose _ file property can be an io.BytesIO (binary mode), io.TextIOWrapper (text mode) object, or a real file object (calling the roller () function). Similarly, this class file object can be used in with context management, just like normal files.
This function also has an additional method, rollover (), which makes the created file scroll through the contents of the disk, regardless of its size. The calling format of the SpooledTemporaryFile function is the same as that of the TemporaryFile () function, which is not covered here.
4. Advanced temporary directory creation function: TemporaryDirectory
The TemporaryDirectory function securely creates temporary directories using the same rules as mkdtemp (). The generated object can be used as a context manager (an example is given here). When the context is completed or the temporary directory object is destroyed, the newly created temporary directory and all its contents are deleted from the file system. The invocation format is as follows:
TemporaryDirectory (suffix=None, prefix=None, dir=None)
After calling this function, the directory name created can be retrieved from the name property of the returned object. When the returned object acts as the context manager, the name is assigned to the target of the as clause in the with statement. In addition, you can explicitly clean up the target by calling the cleanup () method.
5. Underlying temporary file / directory creation functions: mkstemp and mkdtemp
The underlying temporary file creation function mkstemp ()
The mkstemp () function creates temporary files as securely as possible, and unlike the TemporaryFile () function, the user is responsible for deleting temporary files. The invocation format is as follows:
Mkstemp (suffix=None, prefix=None, dir=None, text=False)
Function argument options:
(1) suffix: file suffix. Default is None. If it is not None, the file name ends with the set suffix. Note that the mkstemp () function does not add a dot between the file name and the suffix, but can add it itself if necessary.
(2) prefix: file prefix, default is None. If it is not None, the file begins with this prefix; otherwise, the default prefix is determined by the return value of gettempprefix () or gettempprefixb ().
(3) dir: the directory created by the file. The default is None. If it is not None, the default directory is selected from the list of systems used, but the user can control the directory location by setting the TMPDIR, TEMP or TMP environment variables.
(4) text: text type, default is False, that is, open in binary mode. If true, open it in text format.
Mkstemp () returns a tuple that contains an operating system level (OS-leve) handle for opening a file (returned by the os.open () function) and the file's absolute pathname.
The underlying temporary directory creation function mkdtemp ()
The mkdtemp () function creates a temporary directory in as secure a way as possible. Directories can only be read, written, and searched through the user ID, while the user is responsible for deleting temporary directories and their contents. The invocation format and parameters are the same as mkstemp (), so I won't cover them here. Note that the mkdtemp () function only returns the absolute path to the newly created directory.
3. File / folder attribute manipulation function of tempfile module 1. Return temporary folder name functions: gettempdir and gettempdirb
Gettempdir () returns the name of the folder where the temporary files are stored, and the return value of this function is the global variable within tempfile, that is, the folder name returned by gettempdir () will be the default value for all function dir parameters in the tempfile module.
Python searches internally for a list of standard directories in which you can create files:
(1) the directory named by the TMPDIR environment variable
(2) directories named by the TEMP environment variable
(3) Directory named by the TMP environment variable.
(4) specific locations of different platforms:
Windows system, directory: C:\ TEMP, C:\ TMP,\ TEMP, and\ TMP
For other systems, the directory is / tmp, / var/tmp, and / usr/tmp.
(5) current working directory.
In addition, there is a function gettempdirb () that has the same effect as gettempdir (), except that it returns in binary format.
2. Return temporary file prefix functions: gettempprefix and gettempprefix
Gettempprefix returns the prefix of the created temporary file in text format; gettempprefixb returns the prefix of the created temporary file in binary format.
At this point, the study on "how to use the tempfile module of Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.