Created
June 12, 2023 09:27
-
-
Save NothingCtrl/e56ff996bba93ab7bdd0463faef563c1 to your computer and use it in GitHub Desktop.
Get file md5 hash
This file contains hidden or 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
| # python3 | |
| import sys | |
| import hashlib | |
| import os | |
| def file_hash_info(file_path: str): | |
| with open(file_path, "rb") as f: | |
| file_hash = hashlib.md5() | |
| while chunk := f.read(8192): | |
| file_hash.update(chunk) | |
| # print(file_hash.digest()) | |
| md5_name = os.path.basename(file_path) | |
| print(f"=======\n- MD5 of file {md5_name}: {file_hash.hexdigest()}") # to get a printable str instead of bytes | |
| if __name__ == "__main__": | |
| if len(sys.argv) == 2: | |
| if os.path.isfile(sys.argv[1]): | |
| file_hash_info(sys.argv[1]) | |
| else: | |
| print(f"Not a file: {sys.argv[1]}") | |
| else: | |
| file_name = os.path.basename(os.path.realpath(__file__)) | |
| print(f"Call with file path is required, example: python {file_name} /path/to/file.gz") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment