[Tutor] file io: reading an int32 at the end of a file?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jul 6 22:35:52 CEST 2005



On Wed, 6 Jul 2005, Marcus Goldfish wrote:

> I have a file format that ends in a 4-byte (int32) number.  I would like
> to read this value in python on a WinXP machine with something like:
>
> fname = 'somefile'
> f = open(fname, 'rb')
> f.seek(-4,2)
> offset = f.read()
>
> ... but this doesn't seem to work.  The value that Python returns is:
>
>    '@\x19\x01\x00'


Hi Marcus,

You are very close.  It looks like you're getting the right bytes back as
a single string, so you may need to manually destructure the bytes back
into numbers.  Let's try something:

######
>>> text = '@\x19\x01\x00'
>>> ord(text[0])
64
>>> ord(text[1])
25
>>> ord(text[2])
1
>>> ord(text[3])
0
######

So you are getting the right values: you just need to do a little bit more
work to unpack them.  Using ord() will do the trick, although there is a
better way to do it, which we'll talk about below:


> but I know from similar code in Matlab that the correct sequence is:
>
>    64 25 1 0

In that case, it sounds like Matlab is taking those four bytes and
treating each bytes as a 'byte' integer.  Python has a nice facility for
doing this with the 'struct' module:

    http://www.python.org/doc/lib/module-struct.html

######
>>> import struct
>>> struct.unpack('bbbb', '@\x19\x01\x00')
(64, 25, 1, 0)
######


I'm not sure if Matlab is treating each byte as an unsigned or signed
char, so I'm guessing a signed interpretation at the moment.


If you have more questions, please feel free to ask.



More information about the Tutor mailing list