title: Iterate over 2 files in each folder and compare them
body: <p>I compare two text files and print out the results to a 3rd file. I am trying to make it so the script i'm running would iterate over all of the folders that have two text files in them, in the CWD of the script.</p>
  
  <p>What i have so far:</p>
  
  <pre><code>import os
  import glob
  
  path = './'
  for infile in glob.glob( os.path.join(path, '*.*') ):
      print('current file is: ' + infile)
      with open (f1+'.txt', 'r') as fin1, open(f2+'.txt', 'r') as fin2:
  </code></pre>
  
  <p>Would this be a good way to start the iteration process?</p>
  
  <p>It's not the most clear code but it gets the job done. However, i'm pretty sure i need to take the logic out of the read / write methods but i'm not sure where to start.</p>
  
  <p>What i'm basically trying to do is have a script iterate over all of the folders in its CWD, open each folder, compare the two text files inside, write a 3rd text file to the same folder, then move on to the next.</p>
  
  <p>Another method i have tried is as follows:</p>
  
  <pre><code>import os
  
  rootDir = 'C:\\Python27\\test'
  for dirName, subdirList, fileList in os.walk(rootDir):
      print('Found directory: %s' % dirName)
      for fname in fileList:
          print('\t%s' % fname)
  </code></pre>
  
  <p>And this outputs the following (to give you a better example of the file structure:</p>
  
  <pre><code>Found directory: C:\Python27\test
      test.py
  Found directory: C:\Python27\test\asdd
      asd1.txt
      asd2.txt
  Found directory: C:\Python27\test\chro
      ch1.txt
      ch2.txt
  Found directory: C:\Python27\test\hway
      hw1.txt
      hw2.txt
  </code></pre>
  
  <p>Would it be wise to put the compare logic under the <code>for fname in fileList</code>? How do i make sure it compares the two text files inside the specific folder and not with other <code>fnames</code> in the <code>fileList</code>?</p>
  
  <p>This is the full code that i am trying to add this functionality into. I appologize for the Frankenstein nature of it but i am still working on a refined version but it does not work yet.</p>
  
  <pre><code>from collections import defaultdict
  from operator import itemgetter
  from itertools import groupby
  from collections import deque
  import os
  
  
  
  class avs_auto:
  
      def load_and_compare(self, input_file1, input_file2, output_file1, output_file2, result_file):
          self.load(input_file1, input_file2, output_file1, output_file2)
          self.compare(output_file1, output_file2)
          self.final(result_file)
  
      def load(self, fileIn1, fileIn2, fileOut1, fileOut2):
          with open(fileIn1+'.txt') as fin1, open(fileIn2+'.txt') as fin2:
              frame_rects = defaultdict(list)
              for row in (map(str, line.split()) for line in fin1):
                  id, frame, rect = row[0], row[2], [row[3],row[4],row[5],row[6]]
                  frame_rects[frame].append(id)
                  frame_rects[frame].append(rect)
              frame_rects2 = defaultdict(list)
              for row in (map(str, line.split()) for line in fin2):
                  id, frame, rect = row[0], row[2], [row[3],row[4],row[5],row[6]]
                  frame_rects2[frame].append(id)
                  frame_rects2[frame].append(rect)
  
          with open(fileOut1+'.txt', 'w') as fout1, open(fileOut2+'.txt', 'w') as fout2:
              for frame, rects in sorted(frame_rects.iteritems()):
                  fout1.write('{{{}:{}}}\n'.format(frame, rects))
              for frame, rects in sorted(frame_rects2.iteritems()):
                  fout2.write('{{{}:{}}}\n'.format(frame, rects))
  
  
      def compare(self, fileOut1, fileOut2):
          with open(fileOut1+'.txt', 'r') as fin1:
              with open(fileOut2+'.txt', 'r') as fin2:
                  lines1 = fin1.readlines()
                  lines2 = fin2.readlines()
                  diff_lines = [l.strip() for l in lines1 if l not in lines2]
                  diffs = defaultdict(list)
                  with open(fileOut1+'x'+fileOut2+'.txt', 'w') as result_file:
                      for line in diff_lines:
                          d = eval(line)
                          for k in d:
                              list_ids = d[k]
                              for i in range(0, len(d[k]), 2):
                                  diffs[d[k][i]].append(k)
                      for id_ in diffs:
                          diffs[id_].sort()
                          for k, g in groupby(enumerate(diffs[id_]), lambda (i, x): i - x):
                              group = map(itemgetter(1), g)
                              result_file.write('{0} {1} {2}\n'.format(id_, group[0], group[-1]))
  
  
      def final(self, result_file):
          with open(result_file+'.txt', 'r') as fin:
              lines = (line.split() for line in fin)
              for k, g in groupby(lines, itemgetter(0)):
                  fst = next(g)
                  lst = next(iter(deque(g, 1)), fst)
                  with open('final/{}.avs'.format(k), 'w') as fout:
                      fout.write('video0=ImageSource("old\%06d.jpeg", {}-3, {}+3, 15)\n'.format(fst[1], lst[2]))
                      fout.write('video1=ImageSource("new\%06d.jpeg", {}-3, {}+3, 15)\n'.format(fst[1], lst[2]))
                      fout.write('video0=BilinearResize(video0,640,480)\n')
                      fout.write('video1=BilinearResize(video1,640,480)\n')
                      fout.write('StackHorizontal(video0,video1)\n')
                      fout.write('Subtitle("ID: {}", font="arial", size=30, align=8)'.format(k))
  </code></pre>
  
  <p>using the <code>load_and_compare()</code> function, i define two input text files, two output text files, a file for the comparison results and a final phase that writes many files for all of the differences.</p>
  
  <p>What i am trying to do is have this whole class run on the current working directory and go through every sub folder, compare the two text files, and write everything into the same folder, specifically the <code>final()</code> results.</p>