[Tutor] reading binary file on windows and linux

Adam Bark adam.jtm30 at gmail.com
Sun May 9 19:59:58 CEST 2010


On 9 May 2010 18:33, Jan Jansen <knacktus at googlemail.com> wrote:

> Hello,
>
> I've got some trouble reading binary files with struct.unpack on windows.
> According to the documentation of the binary file's content, at the
> beginning there're some simple bytes (labeled as 'UChar: 8-bit unsigned
> byte'). Within those bytes there's a sequence to check the file's sanity.
> The sequence is (in ascii C-Notation):
> " "
> "\n"
> "\r"
> "\n"
> " "
> I've downloaded the file from the same website from two machines. One is a
> Windows 7 64-Bit, the other one is a virtual Linux machine. Now the trouble
> is while on linux everything is fine, on windows the carriage return does
> not appear when reading the file with struct.unpack.
>
> The file sizes on Linux and Windows are exaktly the same, and also my
> script determines the file sizes correctly on both plattforms (according to
> the OS). When I open the file on Windows in an editor and display the
> whitespaces, the linefeed and cariage-return are shown a expected.
>
> 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:
>     size = struct.calcsize("80B")
>     raw_data = struct.unpack("80B", source.read(size))
>     for i, data in enumerate(raw_data):
>         print i, data, chr(data)
>     source.seek(0, 2)
>     print source.tell()
>
>
> Any suggestions are highly appreciated.
>
> Cheers,
>
> Jan
>

I'd guess that it's because newline in windows is /r/n and in linux it's
just /n. If you read the file as binary rather than text then it should work
the same on both platforms ie use:
open(sys.argv[1], "rb")

HTH,
Adam.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100509/555dfa11/attachment-0001.html>


More information about the Tutor mailing list