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.