[Numpy-discussion] Appending a numpy array to binary file

Robert Kern robert.kern@gmail....
Tue Mar 22 10:22:07 CDT 2011


On Tue, Mar 22, 2011 at 05:37, Alessandro
<alessandro.sanginario@polito.it> wrote:
> Hi everybody,
>
> I'm trying to append, inside a loop, into a binary file some arrays using
>
> numpy.save(file, arr) but wvhen I open it there is only the last array I
> saved.
>
> If I use savetxt it works perfectly. Do you know how can I append data to a
> binary file inside a loop?
>
> Example:
>
> x = arange(10)
>
> fp = open("TempFile","ab")
>
> for i in range(3):
>
> save(fp,x)
>
> fp.close()
>
> Data saved into the file is: [0,1,2,3,4,5,6,7,8,9] but I would like:
> [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 0,1,2,3,4,5,6,7,8,9]

The data is actually saved three times, which you can verify by
looking at the actual bytes in the file. The NPY format is
self-describing. It contains a header that describes the shape and
dtype of the data. So you get three header+data blocks in the file.
You cannot implicitly concatenate three different arrays together by
repeatedly saving them out to a file using np.save(). Using np.load()
once will simply load the first array in the file. You can read all
three arrays by doing repeated np.load() calls on the same file
object:

fp = open('TempFile', 'rb')
for i in range(3):
    print np.load(fp)

-- 
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 NumPy-Discussion mailing list