title: Check that a *type* of file exists in Python
body: <p>I realize this looks similar to other questions about checking if a file exists, but it is different. I'm trying to figure out how to check that a <strong>type</strong> of file exists and exit if it doesn't. The code I tried originally is this: </p>
  
  <pre><code>filenames = os.listdir(os.curdir)
  
  for filename in filenames:
  
     if os.path.isfile(filename) and filename.endswith('.fna'):
          ##do stuff
  </code></pre>
  
  <p>This works to 'do stuff' to the file that ends in .fna, but I need it to check and make sure the .fna file is there and exit the program entirely if not. </p>
  
  <p>I tried this:</p>
  
  <pre><code>try:
  
      if os.path.isfile(filename) and filename.endswith('.fna'):
          ## Do stuff 
  except: 
  
      sys.stderr.write (‘No database file found. Exiting program. /n’)
      sys.exit(-1)
  </code></pre>
  
  <p>But that didn't work, it just skips the whole function if the .fna file isn't there, without printing the error.  </p>