Answers for "how to diff two files in python"

1

python compare two files content

import difflib
import sys

# git-styled output

with open('/tmp/hosts0', 'r') as hosts0:
    with open('/tmp/hosts1', 'r') as hosts1:
        diff = difflib.unified_diff(
            hosts0.readlines(),
            hosts1.readlines(),
            fromfile='hosts0',
            tofile='hosts1',
        )
        for line in diff:
            sys.stdout.write(line)
Posted by: Guest on April-02-2021

Python Answers by Framework

Browse Popular Code Answers by Language