On this page
filecmp
Prev: fcntl | Up: stdlib_module | Next: fileinput
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org — filecmp
Tags: #public #python3 #stdlib
Import
import filecmp
Overview
File and directory comparison utilities (cmp(), cmpfiles(), and dircmp). Use filecmp to verify whether two files have identical contents or to diff two directory trees recursively for sync scripts, backup verification, and test assertions.
Quick Start
import filecmp
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
a = Path(tmp) / "a.txt"
b = Path(tmp) / "b.txt"
a.write_text("same content", encoding="utf-8")
b.write_text("same content", encoding="utf-8")
# shallow=False compares actual file bytes, not just os.stat() metadata
assert filecmp.cmp(a, b, shallow=False) is True
Implementation Walkthrough
1. Comparing Two Directory Trees with dircmp
dircmp categorizes entries into left_only, right_only, same_files, diff_files, and subdirs (which maps subdirectory names to nested dircmp objects):
import filecmp
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
left, right = Path(tmp) / "left", Path(tmp) / "right"
left.mkdir(); right.mkdir()
(left / "keep.txt").write_text("v1")
(right / "keep.txt").write_text("v1")
(left / "changed.txt").write_text("old")
(right / "changed.txt").write_text("new")
(left / "only_left.txt").write_text("x")
dc = filecmp.dircmp(left, right, shallow=False)
assert dc.same_files == ["keep.txt"]
assert dc.diff_files == ["changed.txt"]
assert dc.left_only == ["only_left.txt"]
Gotchas & Boundaries
shallow=Trueis the default: By defaultcmp()anddircmp()treat two files as equal if theiros.stat()signatures (type, size, mtime) match, without reading bytes. Passshallow=Falsewhen accuracy matters.- Internal stat/comparison cache:
filecmpcaches comparison results; callfilecmp.clear_cache()if files are modified within the same second in tests. - Line-level text diffs:
filecmponly tells you whether files differ; use difflib to inspect what changed inside text files.
Common Functions
- filecmp.clear_cache() —
clear_cache()— Clear the filecmp cache. - filecmp.cmp() —
cmp(f1, f2, shallow=True)— Compare two files. - filecmp.cmpfiles() —
cmpfiles(a, b, common, shallow=True)— Compare common files in two directories.
Classes & Objects
- filecmp.dircmp — A class that manages the comparison of 2 directories. (Methods:
.phase0(),.phase1(),.phase2(),.phase3(),.phase4(),.phase4_closure())
Constants & Attributes
filecmp.DEFAULT_IGNORES(list) =['RCS', 'CVS', 'tags', '.git', '.hg', '.bzr', '_darcs', '...
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/fcntl
- tuts/python3/stdlib_module/filecmp/clear_cache
- tuts/python3/stdlib_module/filecmp/cmp
- tuts/python3/stdlib_module/filecmp/cmpfiles
- tuts/python3/stdlib_module/filecmp/dircmp
- tuts/python3/stdlib_module/fileinput
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/fcntl Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/filecmp/clear_cache Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/filecmp/cmp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/filecmp/cmpfiles Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/filecmp/dircmp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fileinput Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/difflib Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; shared tags: #stdlib