First, the operation process of the document
1. Open the file, get the file handle and assign a value to a variable
2. Manipulate the file through a handle
3. Close the file
Second, the opening and closing of the file
A, open--open function of the file
Syntax: Open (File[,mode[,buffering[,encoding[,errors[,newline[,closefd=true]]])
[Parameter description]:
file--file location + file name, need to add quotation marks [note]: If the location of the file is not specified, it is in the current folder by default
Open mode of the mode--file
buffering--can take 0, 1, greater than 1 integers or negative numbers
negative
buffering value |
action |
0 |
< Span style= "font-size:14px" > Access files without a deposit (buffer) |
1 |
The line is stored when the file is accessed Buffer (used in text mode only) |
integers greater than 1 |
|
TD style= "Text-align:center" >
The buffer size of the storage area when accessing the file is system default |
encoding--indicates what encoding is used for the returned data, typically using Utf-8 or GBK
errors--value generally have strict, ignore, when taking strict, character encoding if there is a problem will be error, when the Ignore, the character encoding if there is a problem, the program will ignore, continue to execute the following program
newline--can be obtained with a value of none,\n,\r,\r\n; used to differentiate line breaks, but this parameter is valid only for text mode
closefd--can take true or FALSE, which is true by default
Value of CLOSEFD |
Role |
True |
The file parameter passed in is the filename |
False |
The file parameter is passed to the file descriptor (the file descriptor is a non-negative integer, and a file descriptor is returned in the UNIX kernel's system, when a file is opened) |
Python: File Operations Summary file basic operations