Python: write numpy array (int) to binary file without padding
<p>I have a binary file that was created in Fortran consisting of integer values as records. I want to read these into Python, edit them as lists and save them back to binary as np-arrays. For some reason, however, Python inserts an additional "0" after every record in the file. I guess this is what they call "padding", right? How do I suppress this?</p>

<p>Here's a standalone example:</p>

<pre><code>import numpy as np

content = np.array(range(20))

# Write:
with open('D:/bin_test.dat', 'wb') as write_binary:
    write_binary.writelines(content)

# Read: 
with open('D:/bin_test.dat', 'rb') as read_binary:
    content = np.fromfile(read_binary, dtype=np.int16)

print content

Out: 
[ 0  0  1  0  2  0  3  0  4  0  5  0  6  0  7  0  8  0  9  0 10  0 11
0 12 0 13  0 14  0 15  0 16  0 17  0 18  0 19  0]
</code></pre>

<p>If I read the Fortran binary file via <code>np.fromfile</code> and save it back to binary directly, it works just fine. That's why I guess the problem occurs after conversion from list to numpy array.</p>

<p>Thanks!</p>

<p>Check <code>content.dtype</code>. It looks like it is <code>np.int32</code>, which is generally the default integer type on Windows. You are writing 32 bit integers, but then trying to read them back as 16 bit integers. So every other value in the result is 0.</p>

python python-2.7 numpy io binary