Skip to content

Instantly share code, notes, and snippets.

@egcodes
Created October 6, 2014 10:38
Show Gist options
  • Save egcodes/ea2afca8c1664956f111 to your computer and use it in GitHub Desktop.
Save egcodes/ea2afca8c1664956f111 to your computer and use it in GitHub Desktop.
Read file using bit
import array
class ReadFileUsingBit:
def __init__(self):
fileName = "file.txt"
buffer = ""
for b in self.readBitByBit(open(fileName, 'r')):
buffer += str(b)
tmpBuf = ""
dataBit = array.array('B')
for b in buffer:
tmpBuf += str(b)
if len(tmpBuf) == 8:
dataBit.append(int(tmpBuf[::-1], 2))
tmpBuf = ""
f = file('fileCopy.txt', 'wb')
dataBit.tofile(f)
f.close()
def readBitByBit(self, f):
for b in (ord(b) for b in f.read()):
for i in xrange(8):
yield (b >> i) & 1
if __name__ == "__main__":
readFileBitByBit = ReadFileUsingBit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment