title: Count number of files with certain extension in Python
body: <p>I am fairly new to Python and I am trying to figure out the most efficient way to count the number of .TIF files in a particular sub-directory.</p>
  
  <p>Doing some searching, I found one example (I have not tested), which claimed to count all of the files in a directory:</p>
  
  <pre><code>file_count = sum((len(f) for _, _, f in os.walk(myPath)))
  </code></pre>
  
  <p>This is fine, but I need to only count TIF files.  My directory will contain other files types, but I only want to count TIFs.</p>
  
  <p>Currently I am using the following code:</p>
  
  <pre><code>tifCounter = 0
  for root, dirs, files in os.walk(myPath):
      for file in files:    
          if file.endswith('.tif'):
              tifCounter += 1
  </code></pre>
  
  <p>It works fine, but the looping seems to be excessive/expensive to me.  Any way to do this more efficiently?</p>
  
  <p>Thanks.</p>