Python Glossary Русский

Python 3.4+filecmpfunction

filecmp.clear_cache()

comparing files

Clears the cache of filecmp.cmp() results so the next comparison reads the files again.

Syntax

filecmp.clear_cache()

cmp() remembers the answer by the pair of names and their signatures (size, mtime). If a file is rewritten with the same size within the same second (or on a file system with coarse mtime), the signature does not change and cmp() returns the stale answer even with shallow=False. In long-running processes and in tests that write files quickly, call clear_cache() before comparing. Added in 3.4.

Examples

import filecmp, os
T = (1, 1_700_000_000_000_000_000)  # одно и то же время для обоих файлов
open('a.txt', 'w').write('hello'); os.utime('a.txt', ns=T)
open('c.txt', 'w').write('hello'); os.utime('c.txt', ns=T)
print(filecmp.cmp('a.txt', 'c.txt', shallow=False))  # → True
open('c.txt', 'w').write('HELLO'); os.utime('c.txt', ns=T)  # размер и mtime прежние
print(filecmp.cmp('a.txt', 'c.txt', shallow=False))  # → True — ответ из кеша
filecmp.clear_cache()
print(filecmp.cmp('a.txt', 'c.txt', shallow=False))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy