Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: sum_md5_thing.py
- # Version: 1.0.3
- # Author: Jeoi Reqi
- """
- Description:
- - This script reads an input file, calculates the MD5 sum of each line, and writes the results to an output file.
- Functions:
- - md5sum(string):
- Returns the MD5 sum of a given string.
- - process_file(local_input_filename, local_output_filename):
- Reads an input file, calculates MD5 sum of each line, and writes results to an output file.
- Requirements:
- - Python 3.x
- Usage:
- - To use this script, call the process_file function with the input and output filenames as arguments.
- Additional Notes:
- - Make sure the input file exists and is accessible.
- - The output file will be overwritten if it already exists.
- """
- import hashlib
- def md5sum(string):
- """
- Return the MD5 sum of a given string.
- Args:
- string (str): The input string for which the MD5 sum will be calculated.
- Returns:
- str: The MD5 sum of the input string.
- """
- return hashlib.md5(string.encode()).hexdigest()
- def process_file(local_input_filename, local_output_filename):
- """
- Read input file, calculate MD5 sum of each line, and write results to output file.
- Args:
- local_input_filename (str): The filename of the input file containing the strings.
- local_output_filename (str): The filename of the output file where the MD5 sums will be saved.
- """
- md5_dict = {}
- print("Processing the input file. This may take some time...")
- with open(local_input_filename, 'r', encoding='utf-8') as infile:
- for line in infile:
- # Remove leading and trailing whitespace
- line = line.strip()
- # Skip empty lines
- if line:
- # Compute MD5 sum
- md5_hash = md5sum(line)
- # Store the line and its MD5 hash in the dictionary
- md5_dict[line] = md5_hash
- # Write the dictionary to the output file with custom formatting
- with open(local_output_filename, 'w', encoding='utf-8') as outfile:
- for string, md5_hash in md5_dict.items():
- outfile.write(f'"{string}": "{md5_hash}",\n')
- print("\nProcessing completed. Result saved to:", {global_output_filename}, "\n")
- if __name__ == "__main__":
- global_input_filename = 'input.txt' # Replace with your list filename
- global_output_filename = 'sum_md5_output.txt'
- process_file(global_input_filename, global_output_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement