Trying to compare two text files using python

I have a code in python that creates and compares the content of two text files and tells me when they are different:

#this creates the first file, the one used as a control group of sorts
f=open("text1.txt", "w+") 

while True: 
#the other file keeps updating, so it's inside a loop 
 f=open("text2.txt", "w+") 
#I'm guessing the error is probably here
 read = str(open("text1.txt", "r"))
 read2 = str(open("text2.txt", "r"))
 if read2 != read:
        Notifica = True

Both data and data2 are the html from a website I’m reading with BeautifulSoup, that part is working. However the program keeps thinking the the two text files are different even when they are exactly the same. I think I’m doing this the wrong way, any help?


the open() function returns an object, not the content of the file. you are comparing two references to different file objects. you should read the contents of the file and then compare it. you should do:

read = open("text1.txt", "r").read()
read2 = open("text2.txt", "r").read()