Search code examples
pythondifflib

Type error using difflib with command line arguments?


I'm trying to make a function that generates a diff report. I got it working if I hard code file names, but now I'm trying to turn it into a function that takes in command line arguments: two file names to compare, and an int for how many characters before a line wrap occurs.

Here's my code:

import difflib, re, sys, csv

def create_diff(file1, file2, row_length):

    f1 = open(file1, "r").readlines()
    f2 = open(file2, "r").readlines()

    d = difflib.HtmlDiff(
        wrapcolumn=row_length
    )  

    diff = d.make_file(f1, f2, file1, file2)

    report = open("diff2.html", "w")
    report.write(diff)
    report.close()


if __name__ == "__main__":

    if len(sys.argv) == 4:
        create_diff(sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        print("Arguments expected: file 1, file 2, line length")

When running the code above, I get the following error:

Traceback (most recent call last):
  File "diffReport.py", line 24, in <module>
    create_diff(sys.argv[1], sys.argv[2], sys.argv[3])

  File "diffReport.py", line 14, in create_diff
    diff = d.make_file(f1, f2, file1, file2)

  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\difflib.py", line 1769, in make_file
    context=context, numlines=numlines),

  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\difflib.py", line 2020, in make_table
    fromlist,tolist,flaglist = self._collect_lines(diffs)

  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\difflib.py", line 1888, in _collect_lines
    for fromdata,todata,flag in diffs:

  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\difflib.py", line 1864, in _line_wrapper
    self._split_line(fromlist,fromline,fromtext)

  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\difflib.py", line 1813, in _split_line
    if (size <= max) or ((size -(text.count('\0')*3)) <= max):
TypeError: '<=' not supported between instances of 'int' and 'str'

I don't know how to fix this without hard coding in the file names, and I don't know why it's giving a type error, because the int isn't involved?


Solution

  • Not sure what I did to fix, but this works now:

    # from csv_diff import load_csv, compare
    import difflib, re, sys, csv
    
    
    def create_diff(file1, file2, row_length):
    
        f1 = open(file1, "r").readlines()
        f2 = open(file2, "r").readlines()
    
        d = difflib.HtmlDiff(
        wrapcolumn=100
        )  # Change "wrapcolumn" to how many characters should be displayed per line
    
        diff = d.make_file(f1, f2, file1, file2)
    
        report = open("diff.html", "w")
        report.write(diff)
        report.close()
    
    
    if __name__ == "__main__":
    
        if len(sys.argv) == 4:
            create_diff(sys.argv[1], sys.argv[2], sys.argv[3])
        else:
            print("Arguments expected: file1, file2, row length")