Comparing two files contents in python

0 votes

I have 2 text files and I want to compare content of one with another. Need to check ip in Unique.txt is present or not in CheckIp.txt. My code looks like:

with open("/root/nix_bsd_mac_inventory-master/Unique.txt") as fp:
    for line in fp:
        print(line)
        with open("/root/nix_bsd_mac_inventory-master/CheckIp.txt") as fp1:
            for line1 in fp1:
                line1 = line1.rstrip()
                if line == line1:
                    print (line1 + "Ip is Already Present")
                else:
                    print ("Processing Ip:"+ line)

It seems something I am doing wrong. Can someone please help me?

Sep 11, 2018 in Python by bug_seeker
• 15,510 points
731 views

1 answer to this question.

0 votes

Considering your file "Unique.txt" does just containt the IP you're looking for:

f1 = open('CheckIp.txt', 'r')
data= ''.join(f1.readlines())
f2 = open('Unique.txt','r')
toFind = f2.readline()
if data.find(toFind) != -1:
    print("ip "+toFind+" found!")
else:
    print("ip "+toFind+" not found!")
answered Sep 11, 2018 by Priyaj
• 58,020 points

Related Questions In Python

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,681 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,327 views
0 votes
1 answer

Difference between two lists in python

difference = list(set(list1) - set(list2)) READ MORE

answered May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
2,832 views
0 votes
1 answer

Comparing strings in Python

Convert both the strings to either uppercase ...READ MORE

answered May 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
892 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,464 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,125 views
0 votes
1 answer

Comparing two files contents in python

Considering your file "Unique.txt" does just containt the IP ...READ MORE

answered Sep 20, 2018 in Python by Priyaj
• 58,020 points
14,947 views
0 votes
1 answer

Comparing two files contents in python

This code should work for you: It ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
717 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