Solution: Python Md5 Hashing, trailing newline when reading file

Development | Programming languages
5.0 (1 ratings)
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)
All coments
thumb up
on December 05th, 2018 (10:23 pm)
You must sign in to comment!! LOGIN
We use cookies to give you the best possible experience on our site. By continuing to use the site you agree to our use of cookies. Find out more Accept