Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import os
- import hashlib
- def get_file_hash(file_path):
- with open(file_path, 'rb') as file_object:
- tmp = hashlib.md5()
- buff = file_object.read(4096)
- while buff:
- tmp.update(buff)
- buff = file_object.read(4096)
- return tmp.hexdigest().encode()
- def directory_files_list(directory_path):
- for address, dirs, files in os.walk(directory_path):
- for file in files:
- yield address + '/' + file
- def get_md5_of_folder(path):
- directory_hash = hashlib.md5()
- for file in directory_files_list(path):
- directory_hash.update(get_file_hash(file))
- return directory_hash.hexdigest()
- description = """Проверяет идентичность двух директорий.
- Дирекnории считаются идентичными, если MD5 файлов в них совпадают
- (в том числе для вложенных директорий)"""
- parser = argparse.ArgumentParser(description=description)
- parser.add_argument('first_directory', type=str, help='Первая директория')
- parser.add_argument('second_directory', type=str, help='Вторая директория')
- if __name__ == "__main__":
- args = parser.parse_args()
- first_hash = get_md5_of_folder(args.first_directory)
- second_hash = get_md5_of_folder(args.second_directory)
- result = first_hash == second_hash
- message = f"""Директории {"" if result else "не"} идентичны"""
- print(message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement