python - In Python3, when I'm reading a binary file why does 'b' become prepended to my content? -


for example i'm trying read file follows

fd = open('mydb.dbf', 'rb') print(fd.read(1)) 

the output is:

b'\x03'

i wish '\x03'. character coming from?

there no character. have bytes object, contents single byte \x03.

the print function prints str representation of object. bytes object prints out b'\x03'. b no more part of value quotes (or, matter, backslash, x, or 2 digits).

to convince of fact, try print(len(my_bytes)) or print(my_bytes[0]). length 1; first value (byte) number 3.

(if didn't want bytes object, shouldn't have opened file in binary mode. but, considering first character control-c, did want bytes object.)


Comments