使用第三方包 bitarray,就很简单了
import bitarray as ba bits=ba.bitarray('0000111111') with open('xxx.bin','wb') as f: bits.tofile(f)