diff_bytes: This is one of the functions of difflib module. This fuction is used to compare
to lists a and b which are lists of byte objects. The comparison is done using
a dfunc.
diff_bytes allows you to compare data with unknown or inconsistent encoding. All inputs
except n must be bytes objects and not str. Works by losslessly converting all
inputs (except n) to str.
The output of dfunc is then converted back to bytes, so the delta lines that you receive
have the same unknown/inconsistent encodings as a and b.
SYNTAX
diff_bytes(dfunc,a, b, fromfile=b'', tofile=b'', fromfiledate=b'', tofiledate=b'', n=3,
lineterm=b'\n')
Example:
CODE
import difflib
from difflib import diff_bytes
def diffBinaryFiles(Original, Updated):
with open(Original, "rb") as f:
a = f.read()
with open(Updated, "rb") as f:
b = f.read()
Bytes = difflib.diff_bytes(difflib.context_diff(), a, b,fromfile=b'', tofile=b'',fromfiledate=b'', tofiledate=b'', n=3, lineterm=b'\n')
return not(list(Bytes))
In the above code we have used context diff as the dfunc. The two input files taken
are a and b. And the comparison is made using diff_bytes function.
CODE:
In this Code the dfunc Used is unified diff
import difflib
from difflib import diff_bytes
def diffBinaryFiles(Original, Updated):
with open(Original, "rb") as f:
a = f.read()
with open(Updated, "rb") as f:
b = f.read()
Bytes = difflib.diff_bytes(difflib.unified_diff, a, b,fromfile=b'', tofile=b'',fromfiledate=b'', tofiledate=b'', n=3, lineterm=b'\n')
return not(list(Bytes))
Comments