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 ariaholic
• 7,320 points
68 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,710 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 20 in Python by Mohammad
• 2,820 points
23 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 in Python by Waseem
• 4,160 points
16 views
0 votes
0 answers
+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 3, 2018 in Python by charlie_brown
• 7,710 points
1,497 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,710 points
52 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
607 views
0 votes
1 answer

How do you get the logical xor of two variables in Python?

If you're already normalizing the inputs to ...READ MORE

answered May 29, 2018 in Python by ariaholic
• 7,320 points
1,735 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,710 points
65 views
0 votes
1 answer

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

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,710 points
64 views