Solution: Python Md5 Hashing, trailing newline when reading file

Development | Programming languages
  • 5.0 (1 ratings)
Last updated 12/2018
I am experiencing unexpected behavior with file-reading and hashing (in Python 3.7). I have a file that simply has the text "helloworld" in it, without a newline at the end:
import hashlib

def hashit(inp):
    return hashlib.md5(inp.encode('utf-8')).hexdigest()

from_var = 'helloworld'

with open('input.txt', 'r') as fo:
    from_file =

print(f' from_file      : { repr(from_file) }')
print(f' from_var       : { repr(from_var) }')

print(f' from_file hash : { hashit(from_file) }')
print(f' from_var  hash : { hashit(from_var) }')

Edit input.txt nad delete second line, and run again:
 from_file      : 'helloworld'
 from_var       : 'helloworld'
 from_file hash : fc5e038d38a57032085441e7fe7010b0
 from_var  hash : fc5e038d38a57032085441e7fe7010b0

on November 28th, 2018 (9:45 pm)
What masters said
You must sign in to comment!!
All comments
thumb up
on December 05th, 2018 (10:23 pm)