Question about binary file reading

Chris Rebert clp2 at
Thu Mar 5 00:15:28 CET 2009

On Wed, Mar 4, 2009 at 2:58 PM, vibgyorbits <bkajey at> wrote:
> I'm writing a tool to do some binary file comparisons.
> I'm opening the file using
> fd=open(filename,'rb')
> # Need to seek to 0x80 (hex 80th) location
> # Need to read just 8 bytes and get the result back in hex format.
> print x
> This prints out garbage. I would like to know what am i missing here.

`print x` outputs the raw bytes in the bytestring `x` you just read,
which yes, generally looks like gibberish.
It doesn't magically convert it to hex format. Remember that
bytestrings could just as well contain ASCII in other situations,
which you certainly wouldn't want to see as hex. You'll have to
explicitly/manually do the conversion from bytes->hex.


I have a blog:

More information about the Python-list mailing list