all Python 3filecmpfunction
filecmp.cmp()
comparing files
Compares two files and returns True if they are considered equal; by default it trusts size and modification time.
Syntax
filecmp.cmp(f1, f2, shallow=True) -> bool
With shallow=True (the default) files with the same type, size and mtime are declared equal WITHOUT reading their content — after a time-preserving copy or an edit within the same second that is a false True. Only shallow=False is guaranteed to compare content. Results are cached, and a missing file raises FileNotFoundError rather than returning False.
Examples
import filecmp, os
open('a.txt', 'w').write('hello')
open('b.txt', 'w').write('jello')
st = os.stat('a.txt')
os.utime('b.txt', ns=(st.st_atime_ns, st.st_mtime_ns)) # то же время
print(filecmp.cmp('a.txt', 'b.txt')) # → True — сравнили только размер и время
print(filecmp.cmp('a.txt', 'b.txt', shallow=False)) # → False
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy