Created
July 13, 2017 15:07
-
-
Save jevy/ff49c26fd5a8d607be087ef156c66a5b to your computer and use it in GitHub Desktop.
Simple script to compare MD5s between two dirs and all subdirs to be there all the same files exists (ignores filenames and dir structure)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import hashlib | |
# Origin_dir_files = Build an MD5 hash of files with filenames in original dir (with subdirs) | |
# Destination_dir_files = Do the same for all in the destination dir | |
# Iterate over origin_dir_files to see each hash exists in Destination_dir_files. Print any that aren't | |
def get_all_filenames(root): | |
all_file_names = [] | |
for path, subdirs, files in os.walk(root): | |
for name in files: | |
all_file_names.append((os.path.join(path, name))) | |
return all_file_names | |
def md5(fname): | |
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() | |
# Main | |
parser = argparse.ArgumentParser(description='Compare the MD5s of all files and subdirectories') | |
parser.add_argument('origin_dir', help='original directory with correct data',action='store') | |
parser.add_argument('dest_dir', help='destination directory with same desired data',action='store') | |
args = parser.parse_args() | |
origin_file_names= get_all_filenames(args.origin_dir) | |
dest_file_names = get_all_filenames(args.dest_dir) | |
origin_md5s = {} | |
dest_md5s = {} | |
for file_name in origin_file_names: | |
origin_md5s[md5(file_name)] = file_name | |
for file_name in dest_file_names: | |
dest_md5s[md5(file_name)] = file_name | |
for o_md5 in origin_md5s: | |
try: | |
dest_md5s[o_md5] | |
except: | |
print "Not in destination anywhere: " + origin_md5s[o_md5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment