How to compare two .sql files in python?
Question: I would like to compare the *.sql files in python and capture the difference
in a new file (.sql file)

Are there any packages in python which helps in performing the below tasks.

For example:

### file_1.sql

    
    
     CREATE TABLE Persons
     (
     PersonID int,
     LastName varchar(255),
     FirstName varchar(255),
     Address varchar(255),
     City varchar(255)
     );  
    

### file_2.sql

    
    
    CREATE TABLE Persons
    (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    Salary int,
    JobDetail int
    );
    

Expected Output file ->

### diff.sql

    
    
    CREATE TABLE Persons
    (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    **Salary int,
    JobDetail int**
    );
    


Answer: You can use [difflib](https://docs.python.org/2/library/difflib.html) python
module for this:

    
    
    from difflib import Differ
    from pprint import pprint    
    
    d = Differ()
    result = list(d.compare(open('1.sql', 'r').readlines(), open('2.sql', 'r').readlines()))
    pprint(result)
    

