[Tutor] reading binary file on windows and linux

Steven D'Aprano steve at pearwood.info
Sun May 9 20:06:46 CEST 2010


On Mon, 10 May 2010 03:33:51 am Jan Jansen wrote:
> Hello,
>
> I've got some trouble reading binary files with struct.unpack on
> windows. 
[...] 
> The code I'm using to check the first 80 bytes of the file is:
>
> import struct
> import sys
>
> with open(sys.argv[1]) as source:

You're opening the file in text mode. On Linux, there's no difference, 
but on Windows, it will do strange things to the end of lines. You need 
to open the file in binary mode:

open(sys.argv[1], 'rb') 



-- 
Steven D'Aprano


More information about the Tutor mailing list