The filecmp module compares files and summarizes directory differences without another dependency. The important choice is whether a shallow metadata signature is sufficient or content must be read.
Compare two files by content
from filecmp import cmp
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
left = Path(folder, "a.txt")
right = Path(folder, "b.txt")
left.write_text("Python\n", encoding="utf-8")
right.write_text("Python\n", encoding="utf-8")
assert cmp(left, right, shallow=False)
Read dircmp reports correctly
dircmp separates unique names, common files, subdirectories, and problematic entries. Its attributes are computed lazily. It does not produce a textual diff and should not be treated as a cryptographic integrity check.
Choose the required assurance
Use shallow=False when content must be inspected. For persistent or adversarial integrity checks, compare suitable hashes and record the algorithm. Account for permissions, symbolic links, and access errors.
Keep learning
Continue with file integrity with hashlib. The official Python documentation, accessed July 22, 2026, describes caching, shallow comparison, and directory reports.