title: how to compare two directories and output the changed file in python
body: <p>I have two directories in Linux in which I have two files. I need to compare these two directories and print out the changed file.</p>
  
  <pre><code>folder1:
    file1.txt   file2.txt
  
  folder2:
    file1.txt   file2.txt
  </code></pre>
  
  <p>I need to compare <code>folder1</code> with <code>folder2</code> and print out the file name which is not the same in these two directories. I thought of first using <code>ls</code> command to get the list of the files and put them in a <code>list</code> and then compare them using python's <code>filecmp</code>, something like below:</p>
  
  <pre><code>import os
  import filecmp
  
  folder1_path = "/home/user/folder1/"
  folder2_path = "/home/user/folder2/"
  
  def ls(path):
     all = []
     walked = os.walk(path)
     for base, sub_f, files in walked:           
         for sub in sub_f:           
              entry = os.path.join(base,sub)
              entry = entry[len(path):].strip("\\")
              all.append(entry)
  
         for file in files:          
             entry = os.path.join(base,file)
             entry = entry[len(path):].strip("\\")
             all.append(entry)
      all.sort()
      return all
  
  def folder_diff(folder1_path, folder2_path):
      folder1_list = ls(folder1_path)
      folder2_list = ls(folder2_path)
  
      for item1, item2 in zip(folder1_list, folder2_list):
          res = filecmp.cmp(folder1_path + item1, folder2_path + item2)
  
  data = folder_diff(folder1_path, folder2_path)
  print(data)
  </code></pre>
  
  <p>This is throwing an error <code>FileNotFound</code> while file is present in both the folders. Can anyone please suggest any update on this. Thanks</p>