You could use getattr
:
import hashlib
chosenFile = '/etc/passwd'
finalHash = input('Which hash function?') # raw_input in Python 2
assert finalHash in ['md5', 'sha1'] # Optional
h = getattr(hashlib, finalHash)
h.update(open(chosenFile, 'rb').read())
print(h.hexdigest())
Note that the input must not contain dots or parentheses. If you want to allow the user to input md5()
or so, you'll have to strip the parentheses first.