byte/string representation query #9765
-
Poking around in a .mpy file I noticed it's entrails display differently depending on how I open the file. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The .mpy file format is a binary file format, so it really only makes sense to open it using the binary ('rb') option. Otherwise, it opens it as a text file and decodes the contents as a UTF-8 string object (which could actually cause an error if the file contains invalid UTF-8 sequences). When opened as a binary file, Python reads the contents of the file as a bytes object instead of a string object. The way Python prints a bytes object looks very much like a string - there is just a |
Beta Was this translation helpful? Give feedback.
The .mpy file format is a binary file format, so it really only makes sense to open it using the binary ('rb') option. Otherwise, it opens it as a text file and decodes the contents as a UTF-8 string object (which could actually cause an error if the file contains invalid UTF-8 sequences).
When opened as a binary file, Python reads the contents of the file as a bytes object instead of a string object. The way Python prints a bytes object looks very much like a string - there is just a
b
before the opening"
.