[Numpy-discussion] tofile problems

Paul Probert paulprobert@sbcglobal....
Thu Jul 29 18:40:03 CDT 2010


On 07/28/2010 11:32 PM, Christopher Barker wrote:
> Paul Probert wrote:
>>     I'm trying to write numpy arrays as binary data, to support a legacy
>> file format.  So I open a file and write to it:
>>
>> fp = open('somefile','w')
>> ...
>> oldpos = fp.tell()
>> somenumpyarray.tofile(fp)
>> newpos = fp.tell()
>> diff = newpos - oldpos - somenumpyarray.nbytes
>> if diff != 0:
>>     print 'ahhah! mismatch=',diff
>> ...
>>
>> I'm observing that every once in while I get 'ahhah! mismatch=1', that
>> is, the file position is advanced by one more byte than x.nbytes would give.
>> I'm using 1.4.1 on windows, python 2.5.
>> Any ideas?
>
> yup -- you need to open your file as binary:
>
> fp = open('somefile','wb')
>
> python is adding a carriage return when it sees a newline, to conform to
> windows text file conventions.
>
> -Chris
>
That was it Chris. Thanks much.
Paul



More information about the NumPy-Discussion mailing list