+ 2

what if we add "b" to a mode for text files?

Opening Files, Python I know that adding "b" to a mode opens it in binary mode, which is used for non-text files (such as image and sound files). But what what for text files?

1st Mar 2020, 11:00 AM
Suraj Das
Suraj Das - avatar
5 Respostas
+ 1
Suraj Das nice if you tried it on your own. I told that because many users ask questions here without trying it on their own. Sorry for the above answer caused by the misunderstandment. Well here is the answer, binary mode is used to load a buffer of bites into the stream. Most of the time the data read from the non-text files cannot be encoded properly. But there is no difference between the chunk returned by the normal files when reading using normal and binary mode (as the stored bytes are encodable) You can try the following code file = open("file.txt", "rb").read() --the byte stream print(file.decode()) --prints the file in a proper encoding (not much difference in text files)
1st Mar 2020, 3:09 PM
Seniru
Seniru - avatar
+ 2
Seniru woho wait I tried to find the answer on my own. I also checked the Opening Files topic of the python couse. I also searched this question. But I got nothing. If you don't want to answer, please don't answer ✌
1st Mar 2020, 11:12 AM
Suraj Das
Suraj Das - avatar
+ 2
thank you Seniru
1st Mar 2020, 3:18 PM
Suraj Das
Suraj Das - avatar
1st Mar 2020, 11:06 AM
Seniru
Seniru - avatar