-1
f1 = open("C:/Users/user/Documents/intro.txt", "r")  
f2 = open("C:/Users/user/Desktop/intro1.txt", "r")  
  
i = 0
  
for line1 in f1:
    i += 1
      
    for line2 in f2:
          
        
        if line1 == line2:  
            print("Line ", i, ": IDENTICAL")       
        else:
            print("Line ", i, ":")
            print("\tFile 1:", line1, end='')
            print("\tFile 2:", line2, end='')
        break
f1.close()                                       
f2.close() 

Can this method be used to compare them?
I tried alot of methods but i dont see any solutions If anyone could help me that would be great! I am fairly new to python

Kartik
  • 3
  • 1
  • Check out [this](https://stackoverflow.com/questions/3290292/read-from-a-log-file-as-its-being-written-using-python) thread about writing to a file as it's being written to. As for the static file, you could simply just store the data in some sort of data structure (like a `list`) and use that for comparison :) – gmdev Jun 09 '21 at 17:35
  • Do you need to compare the files line by line and find out all different lines.. or to compare two files whole? I guess in the last case you can simply make and compare hashes of the files with . – Bohdan Jun 09 '21 at 17:50
  • @Bohdan Yes exactly files lines need to be compared line by line so if there is a different line in the the file that is being constantly updated then it should be exported to a different file saying these lines are different – Kartik Jun 09 '21 at 17:56
  • @gmdev Yes i am looking into to the thread you suggested. Thank you! – Kartik Jun 09 '21 at 17:57

2 Answers2

0

The above code would compare every line in the file to every line in the other file for each line. You would want to compare them by the same index.

with open("C:/Users/user/Documents/intro.txt", "r") as f1:
    lines1 = f1.readlines()

with open("C:/Users/user/Desktop/intro1.txt", "r") as f2:
    lines2 = f2.readlines()

length1 = len(lines1)
length2 = len(lines2)

if length1 == length2:
    for i in range(length1):
        line1 = lines1[i]
        line2 = lines2[i]

        if lines1[i] == lines2[i]:
            print("Line ", i, ": IDENTICAL")
        else:
            print("Line ", i, ":")
            print("\tFile 1:", line1, end='')
            print("\tFile 2:", line2, end='')
            break
else:
    print("Unequal length: ", length1, " ", length2)
0

In addition to my upper comment, here is the code to easy make hash for a file:

import hashlib

def hash_file(filename):
   """"This function returns the SHA-1 hash
   of the file passed into it"""

   # make a hash object
   h = hashlib.sha1()

   # open file for reading in binary mode
   with open(filename,'rb') as file:

       # loop till the end of the file
       chunk = 0
       while chunk != b'':
           # read only 1024 bytes at a time
           chunk = file.read(1024)
           h.update(chunk)

   # return the hex representation of digest
   return h.hexdigest()
Bohdan
  • 753
  • 1
  • 9
  • 18