title: Traversing directories to count number of files with a specific string
body: <p>I have a directory with several levels of sub-directories. All the files in the directories are html files (approx. 500 in total), and I'd like to go through each file to see if if contains a "sub_middle_1col" division. I found a great tutorial at palewire.com and have used that as my base. The two difficulties I am having are 1) the code broke when it hit a sub-directory (thinking it was a file), and 2) it would not traverse sub-directories -- that is, it only looks at files not in any sub-directory. I may have solved the first problem by adding in a line (noted below), but can't figure out how to integrate other solutions I've seen (e.g., os.walk) into the code in order to solve the second problem. Any ideas? Thanks in advance for any advice.</p>
  
  <pre><code>import os
  
  path = "./Industries"
  my_library = os.listdir(path)
  out = open("out.txt", "w")
  
  for page in my_library:
      file = os.path.join(path, page)
      if os.path.isfile(file) and file.endswith('.html'):    #I ADDED THIS LINE
          text = open(file, "r")
          hit_count = 0
          for line in text:
              if 'sub_middle_1col' in line:
                  hit_count = hit_count + 1
                  print &gt;&gt;  out, page + " =&gt; " + str(hit_count)  
          print page + " =&gt; " + str(hit_count)
          text.close()
  </code></pre>