Python exercise [7] [compare files and output html files with strong readability]

Keywords: Python encoding shell

Title:

Use python to write a command mydiff that can be executed in the shell, which is used to compare the differences between the two files, and output the html page source code with strong readability, which can be saved in the html file for viewing.

  • Format: mydiff file1 File2 [> save file]

Realization:

  • 1. Use the sys.argv function to obtain the parameters input from the command line for passing parameters
    if len(sys.argv) !=3: # Tips for using 3 output commands if the number of parameters is not
        print('''
        usage:  {} file1 file2 [>] [file to save]
        '''.format(sys.argv[0]))
    else: # When the input parameters meet the requirements for comparison
        ... ...
  • 2. Use the difflib.Htmldiff function to compare, and output the html page source code with strong readability
#The last two parameters of sys.argv are the file names to be compared
file1 = sys.argv[1]
file2 = sys.argv[2]
try:
        with open(file1)as f1 ,open(file2)as f2: # Open two files
                content1= f1.read().splitlines(keepends=True) # Read the file
                content2= f2.read().splitlines(keepends=True)
                diff = difflib.HtmlDiff() # Create a tool object
                result =diff.make_file(content1,content2) # Get file comparison results
                print(result) # Output results, you can see the source code written in html
except Exception as e: # Abnormal output prompt
        print('Error:'+e)
  • 3. Indicate the interpreter location and coding format at the beginning of the document
    #!/usr/local/python3/bin/python3.6
    #encoding=utf-8

    Full code:

    #!/usr/local/python3/bin/python3.6
    #encoding=utf-8
    import sys
    import difflib
    if len(sys.argv) !=3: # Tips for using 3 output commands if the number of parameters is not
        print('''
        usage:  {} file1 file2 [>] [file to save]
        '''.format(sys.argv[0]))
    else: # When the input parameters meet the requirements for comparison
        file1 = sys.argv[1]
        file2 = sys.argv[2]
        try:
                with open(file1)as f1, open(file2)as f2:  # Open two files
                        content1 = f1.read().splitlines(keepends=True)  # Read the file
                        content2 = f2.read().splitlines(keepends=True)
                        diff = difflib.HtmlDiff()  # Create a tool object
                        result = diff.make_file(content1, content2)  # Get file comparison results
                        print(result)  # Output results, you can see the source code written in html
        except Exception as e:  # Abnormal output prompt
                print('Error:' + e)
  • 4. Copy the py file to / usr/local/bin / (this path is already in the environment variable), and increase the execution permission. At this time, you can see the mydiff command by using the tab key to complete
  • 5. Test command, output an html code
  • 6. Redirect to generate an HTML file on the desktop (mydiff / etc / passwd / TMP / paswd > / home / kiosk / desktop / diff.html)
  • 7. Open it with a browser, and you can see the html page with strong readability

Posted by Swedie on Fri, 06 Dec 2019 06:58:15 -0800