try:
    l = pickle.load(open('db'))
except IOError:
    l = []
db = dict(l)
path = '/etc/hosts'
checksum = hashlib.md5(open(path).read()).hexdigest()
if db.get(path, None) != checksum:
    print('file changed')
db[path] = checksum
pickle.dump(list(db.items()), open('db', 'w'))
Python library to detect if a file has changed between different runs?