How can I compare the content of two files in Python

0 votes
I have two text files, and I wish to compare the content of both of them. Need to check ip in Unique.txt is present or not in CheckIp.txt. Can someone help me out?  My code looks like:

[code]

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)

[/code]
Apr 16, 2018 in Python by aryya
• 7,460 points
2,627 views

1 answer to this question.

0 votes
Assuming that your file unique.txt just contains 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 Apr 16, 2018 by charlie_brown
• 7,720 points

Related Questions In Python

0 votes
1 answer

How can I find the square of a number in python?

You can use the exponentiation operator or ...READ MORE

answered May 21, 2019 in Python by Mohammad
• 3,230 points
1,204 views
0 votes
0 answers

How can i get the content in the JSON format while making the GET requests in python?

While making POST requests how can we ...READ MORE

Jun 6, 2019 in Python by Waseem
• 4,540 points
551 views
0 votes
0 answers
–1 vote
2 answers
0 votes
0 answers

How can I find the first occurrence of a sub-string in a python string?

I'd like to locate the first index ...READ MORE

Nov 24, 2022 in Python by sarit
• 1,830 points
550 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
13,505 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
1,390 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,432 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,294 views
0 votes
2 answers

How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
1,442 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