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
Source code: Lib/difflib.py This module provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce information about file differences i...
🌐
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.
🌐
W3Schools
w3schools.com › python › ref_module_difflib.asp
Python difflib Module
The difflib module helps compare sequences, generate deltas, and find close matches.
🌐
GitHub
github.com › enthought › Python-2.7.3 › blob › master › Lib › difflib.py
Python-2.7.3/Lib/difflib.py at master · enthought/Python-2.7.3
#! /usr/bin/env python · · """ Module difflib -- helpers for computing deltas between objects. · Function get_close_matches(word, possibilities, n=3, cutoff=0.6): Use SequenceMatcher to return list of the best "good enough" matches.
Author   enthought
🌐
Czarrar
czarrar.github.io › python-diff
Comparing Python Diff Libraries
I’m comparing the performance here between the difflib that’s part of Python and diff_match_patch that’s from Google. I was interested in extracting the word(s) that have been changed between two strings. I started out by using difflib but found that it did weird things with some sequences.
🌐
Real Python
realpython.com › ref › stdlib › difflib
difflib | Python Standard Library – Real Python
The Python difflib module provides classes and functions for comparing sequences and generating human-readable difference reports.
Find elsewhere
🌐
GitHub
github.com › qiao › difflib.js
GitHub - qiao/difflib.js: Text diff library in JavaScript, ported from Python's difflib module. · GitHub
Text diff library in JavaScript, ported from Python's difflib module. - qiao/difflib.js
Starred by 256 users
Forked by 44 users
Languages   CoffeeScript 99.9% | JavaScript 0.1%
🌐
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, ...
🌐
Parseltongue
parseltongue.co.in › exploring-the-difflib-module-in-python
ParselTongue - Exploring the difflib Module in Python
July 26, 2024 - The difflib module in Python is a powerful tool for comparing sequences, such as strings, lists, or lines of text. It is particularly useful for generating differences or "diffs" between two sequences.
🌐
Pybites
pybit.es › articles › comparing_lists
Comparing Lists with Difflib – Pybites
March 8, 2017 - The code was already getting out of hand. From here I’d need to add code to tell me what’s in text2_split if there isn’t a match and what the differences are. Enter difflib.
🌐
HexDocs
hexdocs.pm › difflib
Difflib v0.1.0 — Documentation
We cannot provide a description for this page right now
🌐
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)
🌐
GeeksforGeeks
geeksforgeeks.org › python › compare-sequences-in-python-using-dfflib-module
Compare sequences in Python using dfflib module - GeeksforGeeks
February 24, 2021 - 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 ·
🌐
Packetcoders
packetcoders.io › diff-ing-the-network-difflib-part-1
Diff`ing the Network (difflib) - Part 1
June 19, 2021 - 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 ! hostname eos-access3 ! spanning-tree mode mstp ! no aaa root !
🌐
Visual Studio Code
code.visualstudio.com › docs › sourcecontrol › overview
Source Control in VS Code
November 3, 2021 - Learn how to use VS Code's integrated Git source control features like staging, committing, branching, merge conflict resolution, and GitHub integration.
🌐
PyPI
pypi.org › project › cydifflib
cydifflib · PyPI
Python :: 3.13 · Report project as malware · The following benchmark compares the performance in the original difflib implementation, the library cdifflib and CyDifflib · You can install this library from PyPI with pip: pip install cydifflib · CyDifflib provides binary wheels for all common platforms.
      » pip install cydifflib
    
Published   Apr 11, 2025
Version   1.2.0
🌐
PyPI
pypi.org › project › difflib-parser
difflib-parser · PyPI
Parser for Python's difflib output.
      » pip install difflib-parser
    
Published   Jan 02, 2026
Version   2.1.1
🌐
Example
example.com.vn › python › ref_module_difflib.asp
Mô-đun difflib của Python
The difflib module helps compare sequences, generate deltas, and find close matches.