Advertisement
Python253

calculate_md5_checksum

Apr 6th, 2024
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: calculate_md5_checksum.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script calculates the MD5 checksum for a given file.
  9.  
  10. It uses the hashlib library to calculate the MD5 hash of the contents of the file
  11. and returns the hexadecimal representation of the hash.
  12.  
  13. Example usage:
  14.    filename = "file.txt"  # File needs to be in the current working directory
  15.    md5_checksum = calculate_md5(filename)
  16.    print("MD5 Checksum For", filename, ":", md5_checksum)
  17. """
  18.  
  19. import hashlib
  20.  
  21. def calculate_md5(filename):
  22.     """
  23.    Calculate the MD5 checksum for a given file.
  24.  
  25.    Parameters:
  26.        filename (str): The name of the file for which to calculate the checksum.
  27.  
  28.    Returns:
  29.        str: The MD5 checksum of the file in hexadecimal format.
  30.  
  31.    Raises:
  32.        FileNotFoundError: If the specified file does not exist.
  33.    """
  34.     with open(filename, "rb") as f:
  35.         md5_hash = hashlib.md5()
  36.         for chunk in iter(lambda: f.read(4096), b""):
  37.             md5_hash.update(chunk)
  38.     return md5_hash.hexdigest()
  39.  
  40. # Example usage:
  41. filename = "file.txt"  # File Needs To Be In The Current Working Directory
  42. md5_checksum = calculate_md5(filename)
  43. print("MD5 Checksum For ",filename,": ", md5_checksum)
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement