目前,此脚本所遇到的唯一错误是PermissionError和FileNotFoundError。某些字符无法正确处理,因为它们是使用编码字符串表示的,这导致了FileNotFoundError。我添加了一个KeyboardInterrupt异常,以防脚本运行很长时间,并且您希望查看累积的结果。运行此脚本的目录将包含名为differenthashes.txt的文件。
要执行,只需在底部的compare()调用中替换'path1'和'path2'。如果您有任何建议或者感觉不适合您的需要,请告诉我。
import os
import hashlib
import time
def hash(file):
f = open(file,'rb')
h = hashlib.md5()
checkEOF = b' '
while checkEOF != b'':
checkEOF = f.read(1024)
h.update(checkEOF)
f.close()
return h.hexdigest()
def hashwalk(d = './'):
errlist = []
hashes = []
cwd = os.getcwd()
os.chdir(d)
walkobject = os.walk('./')
try:
for directory in walkobject:
dir = directory[0]
files = directory[2]
for file in files:
try:
pathfile = os.path.join(dir,file)
digest = hash(pathfile)
hashes.append((pathfile,digest))
except PermissionError as error:
errlist.append((pathfile,error))
except FileNotFoundError as error:
errlist.append((pathfile,error))
except KeyboardInterrupt:
print('Program terminated, results may be incomplete')
os.chdir(cwd)
return [hashes,errlist]
def compare(path1,path2,logerrors = False):
loc1 = hashwalk(path1)
loc2 = hashwalk(path2)
differenthash = set(loc1[0]).symmetric_difference(set(loc2[0]))
log = open('differenthashes.txt','w',encoding='utf-8')
log.write('path hash date modified\n')
for f,h in sorted(differenthash):
if (f,h) in loc1[0]:
print(path1+'\\'+f[2:],h,time.ctime(os.stat(path1+'\\'+f[2:]).st_mtime))
log.write(path1 + ' ' +f[2:] + ' ' + h + ' ' + time.ctime(os.stat(path1+'\\'+f[2:]).st_mtime)+'\n')
else:
print(path2+'\\'+f[2:],h,time.ctime(os.stat(path2+'\\'+f[2:]).st_mtime))
log.write(path2 + ' ' +f[2:] + ' ' + h + ' ' + time.ctime(os.stat(path2+'\\'+f[2:]).st_mtime)+'\n')
if logerrors:
log.write('\n\n'+path1+' errors\n')
for error in loc1[1]:
log.write(str(error) + '\n')
log.write('\n'+path2+' errors\n')
for error in loc2[1]:
log.write(str(error) +'\n')
log.close()
compare('path1', 'path2' ,logerrors=True)