Python study note 7 (file & open)

Source: Internet
Author: User

the main purpose of the two functions is file () and open (), because they are confused when used, what are the differences between the two?
generally, we all know that the basic principle of composition is
with open ("filepath ", "R") as fobj
for line in fobj
.......
file operations are the same as many programming languages . Remember to close open file objects. The file opening method also includes R (read) W (cut-off write) A (append mode). These methods are commonly used, and some special help documents are available. I am confused about the usage of file () and open (). If they are the same, why are they different. First look at English

> For more information, see the English document first.
Both are build-in functions.
File (filename [, mode [, bufsize])
Constructor function for the file type, described further in section 3.9, ''file objects ''. the constructor's arguments are the same as those of the open () built-in function described below.
When opening a file, it's preferable to use open () instead of invoking this constructor directly. file is more suited to type testing (for example, writing "isinstance (F, file )").
Open (filename [, mode [, bufsize])
Open a file, returning an object of the file type described in section 3.9, ''file objects ''. if the file cannot be opened, ioerror is raised. when opening a file, it's preferable to use open () instead of invoking the file constructor directly .....

It can be explained in this way.
File is a File Type constructor. Although files can be opened, it is more recommended to use open. Theoretically, there should be no essential difference between the two.

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.