Through the Difflib module to achieve differences in file content, it is the standard library module python, does not need to be installed separately, the role is to compare the differences between files. and the output HTML document is supported, we can compare the difference between the code and the configuration file with Difflib.
1. Compare the differences of two strings
Print the results of the operation:
Understand what the output is:
"-" symbol: contained in the first sequence, but not in the second sequence;
The "+" symbol: contained in the second sequence, but not in the first sequence;
"" (i.e., empty): Two sequence lines are consistent;
"?" Symbol: Indicates that there is an incremental difference in two sequence rows;
"^" Symbol: marks the difference character that exists for two sequence lines.
1. Output HTML-formatted document (using the Make_file () method of the Htmldiff () class)
When we execute this script we export it to a file that ends in. html (Python check_str.py > Diff.html) and opens the view in the browser.
Small example: For example, when we maintain multiple profiles, we compare the differences between different versions of the configuration file, and we see more clearly the difference configuration items that are added or modified. The idea is to read the contents of the file, line as a segmentation, and then compare the generation of HTML documents (here the passwd file instead of the configuration file)
python-File Content Difference comparison