title: How do I calculate the MD5 checksum of a file in Python?
body: <p>I have written some code in Python that checks for an MD5 hash in a file and makes sure the hash matches that of the original.</p>
  <p>Here is what I have developed:</p>
  <pre><code># Defines filename
  filename = &quot;file.exe&quot;
  
  # Gets MD5 from file 
  def getmd5(filename):
      return m.hexdigest()
  
  md5 = dict()
  
  for fname in filename:
      md5[fname] = getmd5(fname)
  
  # If statement for alerting the user whether the checksum passed or failed
  
  if md5 == '&gt;md5 will go here&lt;': 
      print(&quot;MD5 Checksum passed. You may now close this window&quot;)
      input (&quot;press enter&quot;)
  else:
      print(&quot;MD5 Checksum failed. Incorrect MD5 in file 'filename'. Please download a new copy&quot;)
      input(&quot;press enter&quot;) 
  exit
  </code></pre>
  <p>But whenever I run the code, I get the following error:</p>
  <pre><code>Traceback (most recent call last):
  File &quot;C:\Users\Username\md5check.py&quot;, line 13, in &lt;module&gt;
   md5[fname] = getmd5(fname)
  File &quot;C:\Users\Username\md5check.py, line 9, in getmd5
    return m.hexdigest()
  NameError: global name 'm' is not defined
  </code></pre>
  <p>Is there anything I am missing in my code?</p>