Several modes of Python file reading and writing:
r,rb,w,wb So when reading and writing files, what is the main difference between whether there is a b mark?
File usage identification
When reading a file and reading a file, until the end of document (EOF) is read, the end of the file will not be counted. Python will consider the character converted from the byte \x1A(26) as the end of document (EOF).
Therefore, when using'r' to read binary files, incomplete document reading may occur.
Example:
The binary file has the following data arranged from low to high: 7F 32 1A 2F 3D 2C 12 2E 76
If you use'r' to read, the third byte is read, that is, the end of the file is considered.
If you use'rb' to read according to binary bits, the read bytes will not be converted into characters, thus avoiding the above error.
solution:
Binary files use the binary method to read'rb'
to sum up:
When using'r', if it encounters '0x1A', it is regarded as the end of the file, which is EOF. There is no such problem when using'rb'.
That is: if you write in binary and then read out from a file, if there is '0x1A' in it, only a part of the file will be read, and using'rb' will always read the end of the file.
Knowledge point expansion:
So far, this article on the understanding of the meaning of rb in python is introduced. For more relevant content of what rb in python means, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. Cn!
Recommended Posts