How to make an equivalent to Fortran's 'access=stream' in python
Question: Let's say i'm making a loop, and after each iteration, y want to extend some
array.

iter 1 ------------> iter 2 --------------> iter 3-------------->....

shape=[2,4]---->shape=[2,12]----->shape=[2,36]---->....

in fortran i used to do this by appending the new numbers to a binary file
with:

    
    
    OPEN(2,file='array.in',form='unformatted',status='unknown',access='stream')
    write(2) newarray
    

so this would extend the old array with new values at the end.

i wish to do the same in python. This is my attempt so far:

    
    
    import numpy as np
    
    #write 2x2 array to binfile
    bintest=open('binfile.in','wb')
    np.ndarray.tofile(np.array([[1.0,2.0],[3.0,4.0]]),'binfile.in')
    bintest.close()
    
    #read array from binfile 
    artest=np.fromfile('binfile.in',dtype=np.float64).reshape(2,2)
    

But i can't get it to extend the array. Lets say.. by appeding another
[[5.0,5.0],[5.0,5.0]] at the end,

    
    
    #append new values.
    np.ndarray.tofile(np.array([[5.0,5.0],[5.0,5.0]]),'binfile.in')
    

to make it [[1.0,2.0,5.0,5.0],[3.0,4.0,5.0,5.0]] after the reading. How can i
do this?

The other problem i have, is that i would like to be able to make this without
knowing the shape of the final array (i know it would be 2 x n ). But this is
not so important.

edit: the use of 'access=stream' is only to skip having to read format headers
and tails.


Answer: This does the trick:

    
    
    import numpy as np
    
    #write
    bintest=open('binfile.in','ab')
    a=np.array([[1.0,2.0],[3.0,2.0]])
    a.tofile(bintest)
    bintest.close()
    
    #read
    array=np.fromfile('binfile.in',dtype=np.float64)
    

this way, each time its run, it appends the new array to the end of the file.

