Append 1D numpy array to file with new element in new line
<p>I have a number of numpy arrays which I generate iteratively. I want to save each array to a file. I then generate the next array and append it to the file and so forth (if I did it in one go I would use too much memory). How do I best do that? Is there a way of making us of numpy functions such as e.g. <code>numpy.savetxt</code>? (I couldn't find an append option for that function.)</p>

<p>My current code is:</p>

<pre><code>with open('paths.dat','w') as output:
    for i in range(len(hist[0])):
        amount = hist[0][i].astype(int)
        array = hist[1][i] * np.ones(amount)
        for value in array:
            output.write(str(value)+'\n')
</code></pre>

<p>You could pass the open file (handle) to <code>savetxt</code></p>

<pre><code>with open('paths.dat','w') as output:
    for i in range(len(hist[0])):
        amount = hist[0][i].astype(int)
        myArray = hist[1][i] * np.ones(amount)
        np.savetxt(output, myArray, delimiter=',', fmt='%10f')
</code></pre>

<p><code>np.savetxt</code> opens the file if given a name, otherwise it used the file.</p>

<p>Then iterates on the rows of the array and writes them</p>

<pre><code>for row in myArray:
    f.write(fmt % tuple(row))
</code></pre>

<p>where <code>fmt</code> is the string you give, or one that is replicated to match the number of columns in your array.</p>

python arrays numpy file-io