title: DIFF loop writes differences to multiple files
body: <p>The code below was created to compare before and after snapshots of router configuration changes to ensure change integrity.  The code 'works', in that it does create a file specific to a device name, and the difference of the pre and post change files is captured and written to the file.  However, the problem with the code is that the 'difference' of each 'set' of files is appended to every subsequent diff file; if 20 sets of files exist, the last diff file will contain all 20 differences.  The idea behind the code is capture the difference for each device in a single file.  I'm not sure what I'm missing in the code.  The for loop enumerates correctly through the device_list and closes.  I think the issue is around the captured 'diff' information NOT clearing out after it is written to the specific file, and therefore it is just appended to the next file.  I can't 'see' how to correct it.</p>
  
  <p>I appreciate any and all advice/guidance.  Sorry for such a wordy problem description.</p>
  
  <pre><code>    for n, elem in enumerate(device_list):
          prefilename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "PRE_TEST_" + elem + '.txt')
          postfilename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "POST_TEST_" + elem + '.txt')
          difffilename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "DIFF_" + elem + '.txt')
  
          with open(prefilename, 'r') as f:
              h = f.readlines()
              for line in h:
                  if regex_time_stamp.search(line) is not None:
                      new_line = re.sub(regex_time_stamp, '', line)
                      pre_list.append(new_line)
                  else:
                      pre_list.append(line)
  
          with open(postfilename, 'r') as f:
              h = f.readlines()
              for line in h:
                  if regex_time_stamp.search(line) is not None:
                      new_line = re.sub(regex_time_stamp, '', line)
                      post_list.append(new_line)
                  else:
                      post_list.append(line)
  
          open(difffilename, 'w').close()  # Create the file
  
          with open(difffilename, 'a') as f:
              diff = difflib.unified_diff(pre_list, post_list, fromfile=prefilename, tofile=postfilename)
              f.writelines(diff)
  </code></pre>