0

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.

  • 1
    `file.read(1023)`? – Klaus D. Feb 28 '18 at 14:22
  • 2
    Why are you concerned on how it *prints*? It's binary data. It is not meant to "print". And as you already tested, it still gets *read* correctly, and that should be the only thing that counts. – Jongware Feb 28 '18 at 14:25

1 Answers1

1

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.

jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • @YaaseenMohamed No problem. If you feel the answer solved your question, please consider marking it as accepted. – jdehesa Mar 01 '18 at 15:39