Filecmp.cmp() Ignoring Differing Os.stat() Signatures?
Solution 1:
You're misunderstanding the documentation. Line #2 says:
Unless shallow is given and is false, files with identical
os.stat()
signatures are taken to be equal.
Files with identical os.stat()
signatures are taken to be equal, but the logical inverse is not true: files with unequal os.stat()
signatures are not necessarily taken to be unequal. Rather, they may be unequal, in which case the actual file contents are compared. Since the file contents are found to be identical, filecmp.cmp()
returns True
.
As per the third clause, once it determines that the files are equal, it will cache that result and not bother re-reading the file contents if you ask it to compare the same files again, so long as those files' os.stat
structures don't change.
Solution 2:
It seems that 'rolling your own' is indeed what is required to produce a desirable result. It would simply be nice if the documentation were clear enough to make a casual reader reach that conclusion.
Here's the function I am presently using:
def cmp_stat_weak(a, b):
sa = os.stat(a)
sb = os.stat(b)
return (sa.st_size == sb.st_size and sa.st_mtime == sb.st_mtime)
Post a Comment for "Filecmp.cmp() Ignoring Differing Os.stat() Signatures?"