Question about binary file reading

Scott David Daniels Scott.Daniels at Acm.Org
Thu Mar 5 02:25:30 CET 2009


vibgyorbits 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
>     fd.seek(0x80)
> # Need to read just 8 bytes and get the result back in hex format.
>     x=fd.read(8)
>     print x
> This prints out garbage. I would like to know what am i missing here.
> Basically, I am trying to read
> 8 bytes from location 0x80 from a binary file called "filename"
> Any tips/inputs are welcome.

(1) Put some air into those assignments; spaces are free.
(2) You probably want something like this:
     import binascii
     fd = open(filename, 'rb')
     fd.seek(0x80)
     x = fd.read(8)
     print binascii.hexlify(x)
     fd.close()

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list