title: Python Difflib Deltas and Compare Ndiff
body: <p>I was looking to do something like what I believe change control systems do, they compare two files, and save a small diff each time the file changes. 
  I've been reading this page: <a href="http://docs.python.org/library/difflib.html" rel="nofollow">http://docs.python.org/library/difflib.html</a> and it's not sinking in to my head apparently. </p>
  
  <p>I was trying to recreate this in a somewhat simple program shown below, 
  but the thing that I seem to be missing is that the Delta's contain at least as much as the original file, and more.  </p>
  
  <p>Is it not possible to get to just the pure changes? 
  The reason I ask is hopefully obvious - to save disk space.<br>
  I could just save the entire chunk of code each time, but it would be better to save current code once, then small diffs of the changes. </p>
  
  <p>I'm also still trying to figure out why many difflib functions return a generator instead of a list, what's the advantage there? </p>
  
  <p>Will difflib work for me - or I need to find a more professional package with more features? </p>
  
  <pre><code># Python Difflib demo 
  # Author: Neal Walters 
  # loosely based on http://ahlawat.net/wordpress/?p=371
  # 01/17/2011 
  
  # build the files here - later we will just read the files probably 
  file1Contents="""
  for j = 1 to 10: 
     print "ABC"
     print "DEF" 
     print "HIJ"
     print "JKL"
     print "Hello World"
     print "j=" + j 
     print "XYZ"
  """
  
  file2Contents = """
  for j = 1 to 10: 
     print "ABC"
     print "DEF" 
     print "HIJ"
     print "JKL"
     print "Hello World"
     print "XYZ"
  print "The end"
  """
  
  filename1 = "diff_file1.txt" 
  filename2 = "diff_file2.txt" 
  
  file1 = open(filename1,"w") 
  file2 = open(filename2,"w") 
  
  file1.write(file1Contents) 
  file2.write(file2Contents) 
  
  file1.close()
  file2.close() 
  #end of file build 
  
  lines1 = open(filename1, "r").readlines()
  lines2 = open(filename2, "r").readlines()
  
  import difflib
  
  print "\n FILE 1 \n" 
  for line in lines1:
    print line 
  
  print "\n FILE 2 \n" 
  for line in lines2: 
    print line 
  
  diffSequence = difflib.ndiff(lines1, lines2) 
  
  print "\n ----- SHOW DIFF ----- \n" 
  for i, line in enumerate(diffSequence):
      print line
  
  diffObj = difflib.Differ() 
  deltaSequence = diffObj.compare(lines1, lines2) 
  deltaList = list(deltaSequence) 
  
  print "\n ----- SHOW DELTALIST ----- \n" 
  for i, line in enumerate(deltaList):
      print line
  
  
  
  #let's suppose we store just the diffSequence in the database 
  #then we want to take the current file (file2) and recreate the original (file1) from it
  #by backward applying the diff 
  
  restoredFile1Lines = difflib.restore(diffSequence,1)  # 1 indicates file1 of 2 used to create the diff 
  
  restoreFileList = list(restoredFile1Lines)
  
  print "\n ----- SHOW REBUILD OF FILE1 ----- \n" 
  # this is not showing anything! 
  for i, line in enumerate(restoreFileList): 
      print line
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>UPDATE:</p>
  
  <pre><code>contextDiffSeq = difflib.context_diff(lines1, lines2) 
  contextDiffList = list(contextDiffSeq) 
  
  print "\n ----- SHOW CONTEXTDIFF ----- \n" 
  for i, line in enumerate(contextDiffList):
      print line
  </code></pre>
  
  <blockquote>
    <p>----- SHOW CONTEXTDIFF -----</p>
    
    <hr>
    
    <hr>
    
    <hr>
    
    <p><strong>* 5,9 **</strong></p>
  
  <pre><code> print "HIJ"
  
   print "JKL"
  
   print "Hello World"
  </code></pre>
    
    <ul>
    <li><p>print "j=" + j</p>
    
    <p>print "XYZ"</p></li>
    </ul>
    
    <p>--- 5,9 ----</p>
  
  <pre><code> print "HIJ"
  
   print "JKL"
  
   print "Hello World"
  
   print "XYZ"
  </code></pre>
    
    <ul>
    <li>print "The end"</li>
    </ul>
  </blockquote>
  
  <p><strong>Another update:</strong> </p>
  
  <p>In the old days of Panvalet an Librarian, source management tools for the mainframe, you could create a changeset like this: </p>
  
  <pre><code>++ADD 9
     print "j=" + j 
  </code></pre>
  
  <p>Which simply mean add a line (or lines) after line 9. 
  Then there word words like ++REPLACE or ++UPDATE. 
  <a href="http://www4.hawaii.gov/dags/icsd/ppmo/Stds_Web_Pages/pdf/it110401.pdf" rel="nofollow">http://www4.hawaii.gov/dags/icsd/ppmo/Stds_Web_Pages/pdf/it110401.pdf</a></p>