Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import argparse
  2. import os
  3. import hashlib
  4.  
  5.  
  6. def get_file_hash(file_path):
  7.     with open(file_path, 'rb') as file_object:
  8.         tmp = hashlib.md5()
  9.         buff = file_object.read(4096)
  10.         while buff:
  11.             tmp.update(buff)
  12.             buff = file_object.read(4096)
  13.         return tmp.hexdigest().encode()
  14.  
  15.  
  16. def directory_files_list(directory_path):
  17.     for address, dirs, files in os.walk(directory_path):
  18.         for file in files:
  19.             yield address + '/' + file
  20.  
  21.  
  22. def get_md5_of_folder(path):
  23.     directory_hash = hashlib.md5()
  24.     for file in directory_files_list(path):
  25.         directory_hash.update(get_file_hash(file))
  26.     return directory_hash.hexdigest()
  27.  
  28.  
  29. description = """Проверяет идентичность двух директорий.
  30.                 Дирекnории считаются идентичными, если MD5 файлов в них совпадают
  31.                 (в том числе для вложенных директорий)"""
  32.  
  33. parser = argparse.ArgumentParser(description=description)
  34. parser.add_argument('first_directory', type=str, help='Первая директория')
  35. parser.add_argument('second_directory', type=str, help='Вторая директория')
  36.  
  37.  
  38. if __name__ == "__main__":
  39.     args = parser.parse_args()
  40.     first_hash = get_md5_of_folder(args.first_directory)
  41.     second_hash = get_md5_of_folder(args.second_directory)
  42.     result = first_hash == second_hash
  43.     message = f"""Директории {"" if result else "не"} идентичны"""
  44.     print(message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement