Skip to content

Instantly share code, notes, and snippets.

@nero-dv
Created December 15, 2024 17:26
Show Gist options
  • Save nero-dv/1fcc55f87b69c4aa0ef3fac9514ef5ca to your computer and use it in GitHub Desktop.
Save nero-dv/1fcc55f87b69c4aa0ef3fac9514ef5ca to your computer and use it in GitHub Desktop.
Multiprocess MD5 hash checker in Python
import os
import hashlib
from multiprocessing import Pool, cpu_count
def calculate_md5(file_path):
"""Calculate the MD5 hash of a file."""
try:
with open(file_path, 'rb') as f:
md5_hash = hashlib.md5()
for chunk in iter(lambda: f.read(4096), b""):
md5_hash.update(chunk)
return file_path, md5_hash.hexdigest()
except Exception as e:
return file_path, f"Error: {e}"
def get_bin_files(folder_path):
"""Retrieve all .bin files from the specified folder."""
return [
os.path.join(folder_path, file)
for file in os.listdir(folder_path)
if file.endswith('.bin') and os.path.isfile(os.path.join(folder_path, file))
]
def main():
"""Main function to execute the script."""
folder_path = input("Enter the path to the folder containing .bin files: ").strip()
if not os.path.isdir(folder_path):
print(f"The folder '{folder_path}' does not exist or is not a directory.")
return
bin_files = get_bin_files(folder_path)
if not bin_files:
print(f"No .bin files found in the folder '{folder_path}'.")
return
print(f"Found {len(bin_files)} .bin file(s). Calculating MD5 hashes using {cpu_count()} cores...")
with Pool(processes=cpu_count()) as pool:
results = pool.map(calculate_md5, bin_files)
print("\nMD5 Hash Results:")
for file_path, md5 in results:
print(f"{file_path}: {md5}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment