Answers for "python compare text files"

2

how to compare two text files in python

with open('some_file_1.txt', 'r') as file1:
    with open('some_file_2.txt', 'r') as file2:
        same = set(file1).intersection(file2)

same.discard('n')

with open('some_output_file.txt', 'w') as file_out:
    for line in same:
        file_out.write(line)
Posted by: Guest on May-11-2020
1

python compare if 2 files are equal

>>> import filecmp
>>> filecmp.cmp('file1.txt', 'file1.txt')
True
>>> filecmp.cmp('file1.txt', 'file2.txt')
False
Posted by: Guest on September-01-2020
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

Code answers related to "python compare text files"

Python Answers by Framework

Browse Popular Code Answers by Language