[SciPy-user] scipy.io.numpyio fwrite - appending or updating an array

Brennan Williams brennan.williams@visualreservoir....
Fri Aug 1 04:46:51 CDT 2008


Robert
Thanks for the help.
'rb+' seems to work.
'ab+' or 'ab' is working ok.
'wb' or 'wb+' doesn't work, i.e. everything preceding the position I'm 
writing to in the file becomes zero.
I re-read (slowly this time) the online documentation - in fopen the 
permissions are the same as for the built-in open so
I looked at that and yes, 'w+' will truncate.
However, to me, truncation means truncation "after" but it evidently 
also means truncation "before" as well which just wasn't what I was
expecting (probably due to a Fortran background many years ago).
It also seems strange to me to open a file with 'r+' when I want to 
write to it.
But there you go, it seems to be working.
I will also look at memmap - as the size of my data files gets bigger 
with larger datasets etc. it may well be very useful.

Brennan

Robert Kern wrote:
> On Fri, Aug 1, 2008 at 04:14, Brennan Williams
> <brennan.williams@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.
>
>   



More information about the SciPy-user mailing list