<p>I have a string with a length that is a multiple of 8 that contains only 0's and 1's.  I want to convert the string into a byte array suitable for writing to a file.  For instance, if I have the string "0010011010011101", I want to get the byte array [0x26, 0x9d], which, when written to file, will give 0x269d as the binary (raw) contents.</p>

<p>How can I do this in Python?</p>

You could do something like this:
```
```
>>> s = "0010011010011101"
>>> [int(s[x:x+8], 2) for x in range(0, len(s), 8)]
[38, 157]
```
```