I am generating a binary file. I opened it using hexdump, it looks like this below enter image description here
But when I try to read this file in python using
file = open("lfsr.bin","rb")
data = file.read(10)
print data
It's printing blank/empty But if I do
print repr(data)
It prints
'\x00\x00\x00\x00\x01\x00\x01\x01\x01\x01'
How can I read this file in chunks of 1023? This file is actually an o/p of PRN generator code.
As you can seem those first 10 bytes in the file (or all the bytes, it seems from the image) are either 0x00
or 0x01
. In ASCII, those are all non-printable characters, so if you attempt to print
them as a string you won't see anything - but the bytes are read. Printing it with repr
works because repr
gives you a "string representation of the object", so you can see the actual bytes in there.
As suggested, just do data = file.read(1023)
to read a block of 1023 bytes from the file.
Note: This is made somewhat less confusing in Python 3, where data read from a binary file is returned as a bytes
object and str
is a distinct type with decoded string representation. In that case, printing data
would show something like what you get from repr
in Python, because bytes
objects are not assumed to represent text and so it is preferred to show their contents even if it contains non-printable characters.