How to write/pack a binary string to file in Python
<p>I would like to do a simple operations, but I'm not able to manage it. I have a string of '0' and '1' derived by a coding algorithm. I would like to write it to file but I think that I'm doing wrongly. </p>

<p>My string is something like '11101010000......10000101010'</p>

<p>Actually I'm writing a binary file as:</p>

<pre><code>print 'WRITE TO FILE '
with open('file.bin', 'wb') as f:
    f.write(my_coded_string)

print 'READ FROM FILE' 
with open('file.bin', 'rb') as f:
    myArr = bytearray(f.read())
myArr = str(myArr)
</code></pre>

<p>If I look at the size of the file, I get something pretty big. So I guess that I'm using an entire byte to write each 1 and 0. Is that correct?</p>

<p>I have found some example which use the 'struct' function but I didn't manage to understand how it works. </p>

<p>Thanks!</p>