Created
February 20, 2019 08:30
-
-
Save petrilgner/6952382d1f2d5f9eee838a1530837507 to your computer and use it in GitHub Desktop.
File compare with bit accuracy
This file contains hidden or 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
from bitstring import BitArray | |
import filecmp | |
FILE1 = "testfile.txt" | |
FILE2 = "testfile-cor.txt" | |
if filecmp.cmp(FILE1, FILE2): | |
print("Files are the same...") | |
quit() | |
file1 = open(FILE1, "rb") | |
file2 = open(FILE2, "rb") | |
diff_counter = 0 | |
while True: | |
byte1 = file1.read(1) | |
byte2 = file2.read(1) | |
if byte1 != byte2: | |
diff_counter += 1 | |
bites1 = BitArray(byte1) | |
bites2 = BitArray(byte2) | |
print("Different %d. at %s: 1[%s] 2[%s]" % (diff_counter, file1.tell(), bites1.bin[2:], bites2.bin[2:])) | |
if byte1 == b"" and byte2 == b"": | |
break | |
if byte1 == b"" and byte2 != b"": | |
print("File 1 continues!") | |
break | |
if byte2 == b"" and byte1 != b"": | |
print("File 2 continues!") | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment