About Python file Operations Os.fdopen (), Os.close (), Tempfile.mkstemp ()

Source: Internet
Author: User

Well. Recently in the thing is also related to this, because C base slag slag. It takes a bit of effort to figure out what is almost forgotten.

Each language has a file operation associated with it.

In the example of flask today, I see this sentence. Opened the file operation toss the prologue

    DB_FD, flaskr.app.config['DATABASE'] = tempfile.mkstemp ()

A little inquiry will give you an idea of tempfile as a temporary file module. Operations that contain some temporary files

Tempfile.mkstemp ()

In the old and very old version of Python, the first parameter is the security level returned, the second parameter is the absolute path to the file name. This is very strange, it should be later Python version of this library has changed. Because in the query, found that many places are so written back. However, I call this function to see the function inside the implementation of the time to find that, in fact, it is not.

That is, the first parameter returned by the python2.7 version tempfile.mkstemp () that I used is an FD (file_description) file descriptor, and a file name that contains an absolute path. Some methods in Tempfile create files that are automatically deleted after they are closed, but the temporary files created by mkstemp () will not be deleted, but will not be found and used by other applications. You can close this file by using the Os.close (FD) method after use.

By the way a tempfile is introduced. Temporaryfile () method, which is destroyed after the file is closed after creating the temporary file, try to test it using the following method.

File_obj = Tempfile. Temporaryfile (Dir=os.path.dirname (__file__)) with File_obj as F:    f.write ('  123J12OI3JOIJW')    f.seek (0)    print f.read ()

It will be found that unlike the Tempfile.mkstemp () method, the file appears to have not been created and is actually created and then destroyed after it is closed.

I have to stress a problem here, and I didn't get it at first.

I try to use a statement like this to manipulate an FD

FD, name = Tempfile.mkstemp (Dir=os.path.dirname (__file__)) with FD as F:    Pass
Traceback (most recent):   " /users/piperck/desktop/py_pra/laplace_pra/2016_01/2016_01_pra.py "  in <module>    __exit__

Is it strange that the file object I used to generate with Open was not FD? This is not the right script. So I went to see the source ... It's not really a thing.

The FD file descriptor and the Python file object are not a thing. Only python file objects can be manipulated in a related way.

The Open method we usually use is to create a Python file object that can be manipulated by the Python language. is not an FD object.

Python also provides a way to convert an FD object into a Python file object using

Python_file_obj = Os.fdopen (FD,)
Fdopen (FD, mode= ' R ', Bufsize=none)

The corresponding system provides the Os.fdopen method as above

It can be said that the FD object is a system-level thing, C language also has. Os.fdopen Os.close (FD) and other methods to manipulate the FD object. Python file objects can use the Python file manipulation method. Hey, this should not be confusing.

By the way, recommend an article: http://blog.csdn.net/dreamthen/article/details/17263259 introduction of the relevant Python file operation analysis it's pretty thin. I looked at some of the benefits.

About Python file Operations Os.fdopen (), Os.close (), Tempfile.mkstemp ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.