For starters, you need to pass strings to difflib.SequenceMatcher, not files:

# Like so
difflib.SequenceMatcher(None, str1, str2)

# Or just read the files in
difflib.SequenceMatcher(None, file1.read(), file2.read())

That'll fix your error.

To get the first non-matching string, see the difflib documentation.

Answer from Kenan Banks on Stack Overflow
🌐
Python
docs.python.org › 3 › library › difflib.html
difflib — Helpers for computing deltas
difflib.context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n')¶
🌐
Medium
medium.com › @zhangkd5 › a-tutorial-for-difflib-a-powerful-python-standard-library-to-compare-textual-sequences-096d52b4c843
A Tutorial of Difflib — A Powerful Python Standard Library to Compare Textual Sequences | by Kaidong Zhang | Medium
January 27, 2024 - In this tutorial, we learned and practiced the difflib Python standard library, and explored its powerful capability to compare text sequences. Whether it is to compare versions of files or to find the similarity between strings, difflib can provide a convenient and direct solution.
🌐
Python Module of the Week
pymotw.com › 2 › difflib
difflib – Compare sequences - Python Module of the Week
This example compares two lists of integers and uses get_opcodes() to derive the instructions for converting the original list into the newer version. The modifications are applied in reverse order so that the list indexes remain accurate after items are added and removed. $ python difflib_seq.py Initial data: s1 = [1, 2, 3, 5, 6, 4] s2 = [2, 3, 5, 4, 6, 1] s1 == s2: False Replace [4] from [5:6] of s1 with [1] from [5:6] of s2 s1 = [1, 2, 3, 5, 6, 1] s2 = [2, 3, 5, 4, 6, 1] The sections [4:5] of s1 and [4:5] of s2 are the same s1 = [1, 2, 3, 5, 6, 1] s2 = [2, 3, 5, 4, 6, 1] Insert [4] from [3:4] of s2 into s1 at 4 s1 = [1, 2, 3, 5, 4, 6, 1] s2 = [2, 3, 5, 4, 6, 1] The sections [1:4] of s1 and [0:3] of s2 are the same s1 = [1, 2, 3, 5, 4, 6, 1] s2 = [2, 3, 5, 4, 6, 1] Remove [1] from positions [0:1] s1 = [2, 3, 5, 4, 6, 1] s2 = [2, 3, 5, 4, 6, 1] s1 == s2: True
🌐
GeeksforGeeks
geeksforgeeks.org › python › compare-sequences-in-python-using-dfflib-module
Compare sequences in Python using dfflib module - GeeksforGeeks
February 24, 2021 - Example 1: Python3 · # import required module from difflib import Differ # assign parameters par1 = 'Geeks' par2 = 'geeks!' # compare parameters for ele in Differ().compare(par1, par2): print(ele) Output: - G + g e e k s + ! Example 2: Python3 ·
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › difflib.Differ.html
difflib.Differ — Python Standard Library
difflib » · difflib.Differ · View page source · class difflib.Differ(linejunk=None, charjunk=None)[source]¶ · Differ is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines.
🌐
TestDriven.io
testdriven.io › tips › 43480c4e-72db-4728-8afd-0b0f4f42d4f4
Tips and Tricks - Python - comparing two text files with difflib.HtmlDiff() | TestDriven.io
import difflib from pathlib import Path first_file_lines = Path('first.txt').read_text().splitlines() second_file_lines = Path('second.txt').read_text().splitlines() html_diff = difflib.HtmlDiff().make_file(first_file_lines, second_file_lines) Path('diff.html').write_text(html_diff)
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › _modules › difflib.html
difflib — Python Standard Library
Examples: >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), ... 'ore\ntree\nemu\n'.splitlines(1)) >>> diff = list(diff) >>> print ''.join(restore(diff, 1)), one two three >>> print ''.join(restore(diff, 2)), ore tree emu """ try: tag = {1: "- ", 2: "+ "}[int(which)] except KeyError: raise ValueError, ('unknown delta choice (must be 1 or 2): %r' % which) prefixes = (" ", tag) for line in delta: if line[:2] in prefixes: yield line[2:] def _test(): import doctest, difflib return doctest.testmod(difflib) if __name__ == "__main__": _test()
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_module_difflib.asp
Python difflib Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... import difflib words = ["ape", "apple", "peach", "puppy"] print(difflib.get_close_matches("appel", words, n=1)) Try it Yourself »
🌐
Linux Hint
linuxhint.com › difflib-module-python
How to Use the Difflib Module in Python
August 11, 2021 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Python Module of the Week
pymotw.com › 3 › difflib
difflib — Compare Sequences
January 28, 2017 - This example compares two lists of integers and uses get_opcodes() to derive the instructions for converting the original list into the newer version. The modifications are applied in reverse order so that the list indexes remain accurate after items are added and removed. $ python3 difflib_seq.py Initial data: s1 = [1, 2, 3, 5, 6, 4] s2 = [2, 3, 5, 4, 6, 1] s1 == s2: False Replace [4] from s1[5:6] with [1] from s2[5:6] before = [1, 2, 3, 5, 6, 4] after = [1, 2, 3, 5, 6, 1] s1[4:5] and s2[4:5] are the same after = [1, 2, 3, 5, 6, 1] Insert [4] from s2[3:4] into s1 at 4 before = [1, 2, 3, 5, 6, 1] after = [1, 2, 3, 5, 4, 6, 1] s1[1:4] and s2[0:3] are the same after = [1, 2, 3, 5, 4, 6, 1] Remove [1] from positions [0:1] before = [1, 2, 3, 5, 4, 6, 1] after = [2, 3, 5, 4, 6, 1] s1 == s2: True
🌐
Javatpoint
javatpoint.com › difflib-module-in-python
Difflib module in Python - Javatpoint
Difflib module in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Coderz Column
coderzcolumn.com › tutorials › python › difflib-simple-way-to-find-out-differences-between-sequences-file-contents-using-python
difflib - Simple Way to Find Out Differences Between Sequences / File Contents using Python by Sunny Solanki
August 27, 2022 - It then finds out the list of common subsequences using get_matching_blocks() and prints them. import difflib l1 = [1,2,3,5,6,7, 8,9] l2 = [2,3,6,7,8,10,11] seq_mat = difflib.SequenceMatcher(a=l1, b=l2) match = seq_mat.find_longest_match(alo=0, ...
🌐
Towards Data Science
towardsdatascience.com › home › latest › “find the difference” in python
"Find the Difference" in Python | Towards Data Science
January 21, 2025 - Now, with the Difflib, you potentially can implement this feature in your Python application very easily. The key is to use the get_close_matches() function. Suppose we have a list of candidates and an "input", this function can help us to pick up the one(s) that close to the "input". Let’s have a look at the example below.
🌐
GitHub
github.com › hhvm › difflib
GitHub - hhvm/difflib: Functions and classes for calculating the differences between two sequences of items.
July 1, 2023 - DiffLib\ColoredUnifiedDiff: abstract class for rendering unified diffs. Output may be any type - for example, strings or XHP.
Starred by 11 users
Forked by 8 users
Languages   Hack 98.3% | Shell 1.4% | Brainfuck 0.3% | Hack 98.3% | Shell 1.4% | Brainfuck 0.3%
🌐
SourceForge
epydoc.sourceforge.net › stdlib › difflib.Differ-class.html
difflib.Differ - Epydoc
To the contrary, minimal diffs ... matches preserves some notion of locality, at the occasional cost of producing a longer diff. Example: Comparing two texts....
🌐
Python Pool
pythonpool.com › home › blog › learn python difflib library effectively
Learn Python Difflib Library Effectively - Python Pool
March 23, 2022 - Other than that, we covered difflib’s methods, for example, HtmlDiff, ndiff, context_diff, unified_diff, and get_close_matches.
🌐
Packetcoders
packetcoders.io › diff-ing-the-network-difflib-part-1
Diff`ing the Network (difflib) - Part 1
June 19, 2021 - Think, fetching the configuration via Netmiko and then performing a comparison, for example. Let's look at a quick example on comparing 2 configs, import difflib config1 = open("config1.txt").readlines() config2 = open("config2.txt").readlines() diff = difflib.ndiff(config1, config2) print(''.join(diff),) === transceiver qsfp default-mode 4x10G !