title: Compare large csv files inside 2 folders and print exact difference along with the line number in python
body: <p>I have to compare large csv files inside 2 different folders. The files are stored in same name inside these folders. After comparing I need to print the file name, line number and the field which is different. If any line is missing in one of the csv's then I need to print that as well with filename, line number.</p>
  
  <p>I have a code which could print only the field which is different but not the line number between the two given files. </p>
  
  <pre><code>import csv
  import itertools
  f1= open("file1.csv")
  f2= open("file2.csv")
  csv_f1= csv.reader(f1)
  csv_f2= csv.reader(f2)
  for row1, row2 in zip(csv_f1,csv_f2):
      if row1 != row2:
          print(set(row1)-set(row2))
  </code></pre>
  
  <p>Please help in extending this to folder level and print file name and line number along with the field causing difference. Also I need to print file name and line number for missing lines.</p>
  
  <p>I have tried using line_num function on csv.reader part, but it is returning some other result instead of line number. Any function to find the line number along with difference would be of great help. Thank you.</p>
  
  <p>Edit: I am posting the query and files below as asked by @Adrian </p>
  
  <pre><code>import csv
  import itertools
  f1= open("D:/Users/KPriya4/Downloads/csv1.csv")
  f2= open("D:/Users/KPriya4/Downloads/csv2.csv")
  csv_f1= csv.reader(f1)
  csv_f2= csv.reader(f2)
  line = 0
  for row1, row2 in zip(csv_f1,csv_f2):
      if row1 != row2:
          if not row1:
              print("file1.csv: Row " + str(line) + " is empty")
              continue
          if not row2:
              print("file2.csv: Row " + str(line) + " is empty")
              continue
          print(set(row1)-set(row2))
          print(line)
      line += 1
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/3ZUgu.png" rel="nofollow noreferrer">File</a></p>
  
  <p><a href="https://i.stack.imgur.com/cvXIY.png" rel="nofollow noreferrer">File 2 with difference highlighted</a></p>
  
  <p>Row 5 in file1 with first name as 'Carlos' is missing in file2.</p>
  
  <p>When I run the code, I get the following output.</p>
  
  <p><a href="https://i.stack.imgur.com/6PHJf.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>This has another line too (line 6) which does not have difference. </p>
  
  <p>Forgive me for not posting image as such asI am not allowed to attach an image, it is posting as links only.</p>