I have a code in python that creates and compares the content of two text files and tells me when they are different:
data=str(information) #this creates the first file, the one used as a control group of sorts f=open("text1.txt", "w+") f.write(data) f.close() while True: #the other file keeps updating, so it's inside a loop data2=str(newinfo) f=open("text2.txt", "w+") f.write(data2) f.close() #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 break
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?
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()