how to compare two text files in python

Solutions on MaxInterview for how to compare two text files in python by the best coders in the world

showing results for - "how to compare two text files in python"
Joy
15 Aug 2016
1with open('some_file_1.txt', 'r') as file1:
2    with open('some_file_2.txt', 'r') as file2:
3        same = set(file1).intersection(file2)
4
5same.discard('\n')
6
7with open('some_output_file.txt', 'w') as file_out:
8    for line in same:
9        file_out.write(line)
Lorenzo
22 Sep 2017
1import difflib
2import sys
3
4# git-styled output
5
6with open('/tmp/hosts0', 'r') as hosts0:
7    with open('/tmp/hosts1', 'r') as hosts1:
8        diff = difflib.unified_diff(
9            hosts0.readlines(),
10            hosts1.readlines(),
11            fromfile='hosts0',
12            tofile='hosts1',
13        )
14        for line in diff:
15            sys.stdout.write(line)
queries leading to this page
compare two files python scripthow to compare the contents in the txt file pythoncheck if two files are equal pythonpython how to check two filesprint differences in two files pythonhow to compare two string in python from fileshow to check whether contents in two files are same or not in pythoncompare values from two files pythonhow to compare python fileshow to compare txt files in pythoncomparing 2 files in python how to compare two files in python and see of there are differencescompare 2 python fileshow to compare 2 files in pythonpython script to compare 2 text filescompare two python filescompare data in file pyhow to compare two string in python from files line by linepython compare filescompare two files python and get the differencepython library to compare two filescompare multiple txt files and find which lines are different in pythonmultiple file comparison in pythonline by line diff pythoncompare 2 files in python and print differencepython3 code to compare two files line by linepython script to compare the two text files entered by useprint two different file content pythonhow to compare two txt files pyhtonfile line by line comparison pythonpython file compare codehow to compare entire txt file at once pythonwrite a python code to check diff between two fileshow to compare tow files in pythonpython diff two text filescompare multiple text files and find which lines are same in pythonhow to compare 2 text files in pythoncompare two files pythonpython code to compare two different files being read by pythonhow to compare files using pythonhow to create score by comparing two text files in pythonmatch 2 files with python3how to compare files in pythonhow to read file and compare values pythonpython compare two files and return the differencecompare two files in pythonhow to compare file contents in python and where it is modifiedpython file comparehow to comparer files in pythonpython code to compare two text filespython compare two files contenthow do i compare to text files in python 3compare 2 files pythonpython compare two filescomparing files and see the difference pythoncompare two files and find differences pythoncompare data in txt file pythoncompare 2 files using pythoncompare files in pythoncompare 2 text files pythoncompare txt files pythonpython code to compare data of two filesfile compare in pythonpython to compare two filespython compare data in two filescompare 2 file text pythoncompare in txt file pythonfile comparison using pythonhow to use any to compare two strings in pythonpython code compare two files onlinecompare 2 text files in pythonhow to compare two python filescompare 2 ltxt pythondiff txt in pythoncompare to files python and get the difference python compare multiple filescomparison of two text files pythoncompare files python line by linecompare multiple txt files and find which lines are same in pythonpython compare files contenthow to check whether the content of files is same in pythonscript python pour compare two text filessee if 2 text files have a matching word pythoncompare 2 txt files and find which lines are different in pythoncode compare python scriptscompare two files using python match line 1 in file 1 to file 2compare text file pythoncompare string and python inputcomparing two python filescompare string file in pythoncomparing two txt files for same lines pythondiff 2 files pythoncomparing files pythoncompare two code files in pythonhow to compare two files line by line in pythonfile compare code in pythonhow to print on different lines pythonpython compare data fileshow to compare to text file content and print the same content in pythonpython compare 2 files and return the differencepython comparing two text filescompare two py filepython file compare line by linefile handling python compare 2 filescomapring two files in pythonhow to compare the content of two files in pythoncompare files pythoncompare 10 files pythonpython program to compare two filescomparing two txt files for same lines python and removing different filespython 2 files is samepython compare text fileshow to compare value in txt file using pythoncompare a txt file with all other files pythoncompare two python files onlinepython difflib compare two text files return differencehow to diff two files in pythoncompare two file pythoncompare two text files in pythonhow to compare text file in pythonpython best way to compare 2 text filescompare 2 text files for differences and print them in pythonhow to compare text from files in pythoncompare two scripts pythonpython diff two fileshow to compare contents of two files in pythondetect multiple faces in image pythoncompare file with string pythonhow to have two text files in pythonpython compare two text files and return the differencehow to compare one file with other two files in pythoncompare two files using pythonpython compare to file txthow to compare to txt files in pythonpython compare two text files return differencecompare two text files and output difference pythonpython compare file contents with stringpython compare 2 text fileshow to compare two files in with statement in pythonpython run text file against another file and comparehow to compare two text files in pythonfile compare pythonpython compare two files word by wordhow to compare and copy two text files in pythoncomparing two files in pythonhow to compare file in pythonompair 2 files in pythonread lines comparison in pythoncomparing two files in python texthow to display two videos in opencv pythoncompare python fileshow to compare two files in pythonhow to compare two text files in python