Overview:
- The function context_fiff() emulates the behavior of the diff command invoked with the -c or -C switch.
diff -C <context line count > <fromfile> <tofile>
The context format of diff command:
- The context difference format lists a block of changes in fromfile followed by a block of changes in tofile. In This scheme of listing the changes using pairs of blocks the first pair of the block depicts fromfile changes and the second pair of the block depicts tofile changes. The blocks continue till both the files are exhaust.
- The lines without any prefix are the ones that have not changed. These lines are the context lines.
- In context format, the context lines are shown both in fromfile changes and tofile changes. On the contrary the unified format shows the changes in both files using single blocks thus avoiding redundancy of context lines.
- The prefix “+” denotes an added line and a “-” denotes a deleted line. The prefix “!” shows an intra-line change.
Example:
|
# Example Python program that displays the import difflib # Text lines to be compared text2 = ["And learn, too late, they grieved it on its way,", # Find the differences in context diff format # Print the differences in context diff format |
Output:
|
The contextual difference: --- to.txt *************** *** 1,2 **** ! Because their words had forked no lightning they ! And learn, too late, they grieved it on its way, |