[Tutor] Windows problem with large(?) files

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Feb 2 11:30:15 CET 2006



On Thu, 2 Feb 2006, johan nilsson wrote:


> apparently the XP box thinks the file is alot shorter, and that can't be
> as it is the same file from the same media (USB stick). What is going
> wrong here?

Hi Johan,

Is it possible that you need to treat the file as a binary file?  It's
rare, but very possible that something like:

    f = open(somefilename,'r')
    a = f.read()

will give you different results on different platforms, because newline
translation occurs on the Windows end of things.  ("\n" --> "\r\n" or visa
versa)  So if your file coincidently has bytes with the sequential values:

######
>>> ord('\r'), ord('\n')
(13, 10)
######

then we should expect to see those two bytes collapsed down to a single
one through the mechanism of newline translation.


But if this is happening, there's an easy fix:

    f = open(somefilename,'rb')
    a = f.read()

where we open the file in binary mode.


Good luck!



More information about the Tutor mailing list