python function that returns the md5sum of a file. write comments & google style…
- User
python function that returns the md5sum of a file. write comments & google style docstring. Optmise for readability & speed & memory
Assistant
import hashlib def md5sum_file(file_path, buffer_size=8192): """Computes the MD5 checksum of a file using memory-efficient chunked reading. Reads the file in binary mode with configurable buffer size to handle large files efficiently while maintaining speed. Args: file_path (str): Absolute or relative path to the target file buffer_size (int, optional): Read buffer size in bytes. Defaults to 8192. Returns: str: Hexadecimal string representation of the MD5 checksum (32 chars) Raises: FileNotFoundError: If file_path doesn't exist OSError: For file access errors (permissions, invalid path, etc.) Example: >>> md5sum_file('example.txt') 'd41d8cd98f00b204e9800998ecf8427e' """ # Initialize MD5 hash object hash_md5 = hashlib.md5() # Open file in binary mode for consistent reading across platforms with open(file_path, "rb") as file: # Read file in chunks using iterator pattern to avoid loading entire file for chunk in iter(lambda: file.read(buffer_size), b''): hash_md5.update(chunk) return hash_md5.hexdigest()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.