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

Bob Gailer bgailer at sbcglobal.net
Wed Jul 6 23:17:53 CEST 2005


At 01:12 PM 7/6/2005, Marcus Goldfish wrote:
>Hi,
>
>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'
>
>but I know from similar code in Matlab that the correct sequence is:
>
>    64 25 1 0
>
>Can someone point out my error?

A file is a sequence of characters. That is what Python is showing you. 
Since 3 of them are not "graphic characters" Python displays their 
hexadecimal equivalents.. x19 translated to base 10 is 25. To ask Python to 
(in essence) cast a character to an integer you use the ord function. 
Ord('\x19') will give you integer 25.


>Thanks!
>Marcus
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor

Bob Gailer
mailto:bgailer at alum.rpi.edu
510 558 3275 home
720 938 2625 cell  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050706/9cb1ce93/attachment.htm


More information about the Tutor mailing list