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