Created
October 6, 2014 10:38
-
-
Save egcodes/ea2afca8c1664956f111 to your computer and use it in GitHub Desktop.
Read file using bit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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