Diff_bytes in difflib Module














































Diff_bytes in difflib Module




diff_bytes

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')



  1. dfunc: Objects are compared using dfunc ,either unified_diff() or context_diff().dfunc
    must be a callable.
  2. a & b: List of byte objects to be compared.
  3. fromfile & tofile :  A header for file names
    that are compared. If not specified, the strings default to blanks. The b
    prefix in the syntax will will produce an instance of the bytes type.
  4. Fromfiledate & tofiledate : The modification times of the files that are compared. The
    modification times are normally expressed in the
    ISO 8601 format. If not
    specified, the strings default to blanks.
    The b prefix in the syntax will will
    produce an instance of the bytes type.

  5. n :  The number of context lines. By default the
    value of n is set to 3. Only this parameter is not converted to byte objects.
  6. lineterm : This is for the inputs that do not have the trailing newlines. We can set the
    lineterm argument, so that the output will be uniformly newline free.
    The b
    prefix in the syntax will will produce an instance of the bytes type.

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