Python script to compare 2 files

+1 vote
Can you please let me know if you have some python script that compares 2 text files, irrespective of the type and write the difference between that 2 files in text format in some other file.
Jul 31, 2019 in Python by Shubhangi
12,348 views

1 answer to this question.

+1 vote

Refer the given code and let us know in case you have any concerns.​

with open('first_file.txt', 'r') as file1:
    with open('second_file.txt, 'r') as file2:
        difference = set(file1).difference(file2)
        difference.discard('\n')
        with open('diff.txt', 'w') as file_out:
        for line in difference:
            file_out.write(line)


Hope this helps!!

If you need to know more about Python, join Python certification course today.

Thanks!

answered Jul 31, 2019 by Arpit
Did this work for you?
Yes, it did. My requirement was a little different so modified accordingly.
Thank you it is working well.
This was exactly what I needed, Thank you.

Related Questions In Python

–1 vote
2 answers
0 votes
1 answer

How can I compare the content of two files in Python?

Assuming that your file unique.txt just contains ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,660 views
0 votes
1 answer

How to import other Python files?

importlib is recent addition in Python to ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 58,020 points
883 views
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 5, 2018 in Python by Omkar
• 69,220 points

edited Aug 8, 2018 by Omkar 8,154 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,440 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,076 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
15,280 views
0 votes
1 answer

I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.

You don't have a template matching tabularQueryResult in your ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 58,020 points
1,450 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP