Question about binary file reading

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Mar 4 19:03:30 EST 2009


vibgyorbits <bkajey at gmail.com> writes:

> 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.

Are you missing anything? Perhaps those bytes, when printed in your
terminal's encoding, *are* garbage. Are you expecting them to be
encoded text, or something else?

> Basically, I am trying to read 8 bytes from location 0x80 from a
> binary file called "filename"

You have successfully done that. What else do you want to do with
those bytes once read?

Perhaps a better question: What goal are you trying to accomplish?

-- 
 \        “Pinky, are you pondering what I'm pondering?” “Wuh, I think |
  `\   so, Brain, but wouldn't anything lose its flavor on the bedpost |
_o__)                               overnight?” —_Pinky and The Brain_ |
Ben Finney



More information about the Python-list mailing list