import hashlib import os import shutil import sys from datetime import time def md5(fname): if not os.path.isfile(fname): return "" hash_md5 = hashlib.md5() with open(fname, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() if __name__ == '__main__': if (len(sys.argv) < 4): print("参数不足") sys.exit(1) sub_path = sys.argv[1] orgion = '{}/{}'.format(sub_path, sys.argv[2]) cacheOrgion = '{}/{}'.format(sub_path, sys.argv[3]) cacheDirty = '{}/{}'.format(sub_path, sys.argv[4]) if len(sys.argv) > 5: # 后处理 if not os.path.exists(cacheDirty): print('cacheDirty文件不存在,直接复制orgion到cacheDirty') dir_path = os.path.split(cacheDirty)[0] if not os.path.exists(dir_path): os.makedirs(dir_path) print('后处理') print('{} => {}'.format(orgion, cacheDirty)) shutil.copy(orgion, cacheDirty) sys.exit(0) hash1 = md5(orgion) hash2 = md5(cacheOrgion) if hash1 == hash2: print('两个文件MD5值相同,文件内容相同') if os.path.exists(cacheDirty): shutil.copy(cacheDirty, orgion) print('{} => {}'.format(cacheDirty, orgion)) else: print('两个文件MD5值不同,文件内容不同') if not os.path.exists(cacheOrgion): print('cacheOrgion文件不存在,直接复制orgion到cacheOrgion') dir_path = os.path.split(cacheOrgion)[0] if not os.path.exists(dir_path): os.makedirs(dir_path) shutil.copy(orgion, cacheOrgion) print('{} => {}'.format(orgion, cacheOrgion))