write numpy array with its size to binary file write numpy array with its size to binary file numpy numpy

write numpy array with its size to binary file


You can use numpy.savetext if you want to save it as ascii.

Alternatively (since it looks like you're dealing with binary data), if you want to save the raw data stream, you could use ndarray.tostring to get a string of bytes that you can dump to the file directly.

The advantage of this approach is that you can create your own file format. The downside is that you need to create a string in order to actually write it to the file.


And since you say that you're getting an error on the second line, it's an error because f.write expects a string. You're trying to pass it a tuple or ints. You could use struct.pack to solve this problem:

f.write(struct.pack('2i',*array.shape))