title: comparing a txt file with csv file with python
body: <p>I am working on developing a python code that would compare a txt file and a csv file and find out if there are identical or not.If not,find the errors and summarize them on a excel table.</p>
  
  <pre><code>def main(): 
  filename1=input("Enter txt file name:- ") 
  filename2=input("Enter csv file name:- ")
  
  fp1=open(filename1,"r") 
  fp2=open(filename2,"r")
  
  list1=[] 
  list2=[]
  
  for line in fp1: #iterating through each line in the file
     a=line.split(",") #splitting line based on comma
     for i in a: #iterating through each element in list
         i=i.rstrip() #removing new line from elements in list
         list1.append(i) #appending element in the list
  
  for line in fp2:
     a=line.split(",")
     for i in a:
         i=i.rstrip()
         list2.append(i)
  
  fp2=open("res.csv","a") #opening res file in append mode
  flag=0
  
   for i in range(0,len(list1)): #iterating through lists
  
     if (i==len(list1)-1 and list1[i]!=list2[i]): #if total is different in both files
         fp2.write(list1[i-1]+","+str(abs(int(list1[i])-int(list2[i])))) #printing difference
         flag=1
     elif (list1[i]!=list2[i]): #if other lines are different
         fp2.write(list1[i-1]+","+list1[i]+","+list2[i-1]+","+list2[i]+"\n") #printing different lines
         flag=1
  
  if (flag==0): #if there is no difference
     fp2.write("none")
  
   main() #calling main function
  </code></pre>
  
  <p>The output should be an excel table with a summary of the differences between the 2 files.The above code gives the difference in numbers between the files but if the number of lines is different, the output should also print the lines that are not in 1 file. I would appreciate any ideas to improve this code and help creating the code to compare a txt and csv file with an output of the difference on a excel spreadsheet.
  Thank you.
  * I am still new here so please let me know if I need to edit something or make a part of my question more clear.</p>