[Numpy-discussion] tofile problems
Wed Jul 28 23:32:08 CDT 2010
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()
> 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.
Christopher Barker, Ph.D.
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
More information about the NumPy-Discussion