Currently the only errors this script accounts for is PermissionError and FileNotFoundError. Certain characters cannot properly be handled since they are represented using their encoding string and this resulted in a FileNotFoundError. I added a KeyboardInterrupt exception in case the script is running long and you want to see the accumulated results. The directory this script is ran from will contain a file called differenthashes.txt.
To execute just replace 'path1' and 'path2' in the compare() call at the bottom. Let me know if you have any suggestions or don't feel this properly accounts for your need.
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)