ID EN
Diff Tools

unified_diff

Python 3.11

Compare a and b (lists of strings); return a delta (a generator generating the delta lines) in unified diff format.

Syntax

PYTHON
difflib.unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n')

Examples

Example 1
PYTHON
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> sys.stdout.writelines(unified_diff(s1, s2, fromfile='before.py', tofile='after.py'))
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido

See Also

generator io.IOBase.readlines() io.IOBase.writelines() A command-line interface to difflib