[SciPy-user] scipy.io.numpyio fwrite - appending or updating an array
Robert Kern
robert.kern at gmail.com
Fri Aug 1 05:30:40 EDT 2008
On Fri, Aug 1, 2008 at 04:14, Brennan Williams
<brennan.williams at visualreservoir.com> wrote:
> I'll look into memmap as Stefan suggested. If Robert Kern's out there,
> do you have any comments about what I might be doing wrong.
I think you want 'rb+'. 'ab+' puts you at the end of the file for
writing (because you asked to append). I believe the '+' in 'wb+' is
simply ignored, so you are getting the truncating behavior of 'wb'.
I've only tested 'rb+' with file.write(), but ultimately, both
file.write() and scipy.io.fwrite() both use C's fwrite(3) down at the
bottom.
In [40]: f = open('foo.dat', 'wb')
In [41]: f.write('Foo!' * 4)
In [42]: f.close()
In [43]: open('foo.dat', 'rb').read()
Out[43]: 'Foo!Foo!Foo!Foo!'
In [44]: f = open('foo.dat', 'rb+')
In [45]: f.tell()
Out[45]: 0L
In [46]: f.seek(4)
In [47]: f.tell()
Out[47]: 4L
In [48]: f.write('Bar!')
In [49]: f.tell()
Out[49]: 8L
In [50]: f.close()
In [51]: open('foo.dat', 'rb').read()
Out[51]: 'Foo!Bar!Foo!Foo!'
But yeah, look at memmap arrays. Much nicer.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the SciPy-User
mailing list