1. When talking about Python file operations, be sure to first say the function open () of the operation file in Python and say about the arguments in the open () function according to the two Open methods encoding
With open (' La.txt ', ' R ', encoding= ' utf-8 ') as F:
Pass
With open (' la.txt ', ' RB ') as F:
Pass
First, when opened according to "R" mode, you can increase the encoding (encoding) parameter, do not increase the default encoding= ' Utf-8 ', and in "RB" mode is opened without the need to encode (encoding) parameters, this is why, People think about the file on the hard disk is stored in bytes as binary, and in the "R" mode when opened, you need to read the string, and from the hard disk to get a byte, so Python needs to convert the bytes into a string, the process needs to encode, you need to encode bytes in what encoding into a string, While this process is open in this mode, Python does so by default. When opened in "RB" mode, you need to read bytes, and the hard drive is the byte itself, so no coding is required.
Okay, here's the r+ w+ A + three open mode:
- r+ mode when opening a file:
1. When opening a file, the file pointer is at the beginning of the 0 position, F.tell () = 0
2. If read (1) specifies a specific parameter in read, where 1 means reading a character, after reading, the pointer jumps to the specified position and continues reading from that position if read continues
3. If there is write () in the read process, the pointer will jump to the last, and the appended way to write
1.w+ means to write and read
2. When opening a file in w+ mode, clear the contents of the file, the file pointer is 0-bit
3.write () After the file pointer jumps to the last
4. If you want to read the content you need to adjust the pointer position F.seek = 0
1.a+ means to write and read
2. After opening the file in w+ mode, the file pointer jumps to the last
3.write (), the content is appended
4. If you want to read the content you need to adjust the pointer position F.seek = 0
Finally, for F.seek,f.tell values are bytes
Python file operations