Find difference between two text files in Python

#!/usr/bin/python
import difflib, sys

file1 = sys.argv[1]
file2 = sys.argv[2]

diff = difflib.unified_diff ( open(file1).readlines(), open(file2).readlines(), fromfile=file1, tofile=file2 )

strings = list()

for line in diff:
    strings.append(line.replace('\n', ''))

for string in strings:
    print(string)

This script compares two text files in Python using difflib and prints a unified diff, the same format version control tools use. You pass the two file paths as arguments.

difflib ships with the standard library, so nothing needs installing, and it is handy for comparing generated output, logs, or configuration against an expected version.

Comments

Popular posts from this blog

[Solved] Error: No such keg: /usr/local/Cellar/gcc

[How To] Unfollow Non-followers on Instagram