Hi, I was a little confused about the iteration behaviour when open a file in binary mode. Why Python default iterates to read bytes by ‘\n’ rather than reading by fixed-size chunk of bytes ? I thought ‘\n’ likely related to “text” mode. Can anyone explains it? Thank you very much!
1 Like
Text mode reads the bytes from the file and decodes them into unicode.
Binary mode just reads the bytes.
The data read in binary mode may be text and then the \n works well.
If it is not text in nature there is no reasonable default that can be used.
You will be using read(size) not an iterator.
It shouldn’t change use of your file so much, just to add a ‘b’ to the open.