title: Loop through folders in Python and for files containing strings
body: <p>I am very new to python. 
  I need to iterate through the subdirectories of a given directory and return all files containing a certain string.</p>
  
  <pre><code>for root, dirs, files in os.walk(path):
      for name in files:
          if name.endswith((".sql")):
              if 'gen_dts' in open(name).read():
                  print name
  </code></pre>
  
  <p>This was the closest I got. </p>
  
  <p>The syntax error I get is </p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;pyshell#77&gt;", line 4, in &lt;module&gt;
      if 'gen_dts' in open(name).read():
  IOError: [Errno 2] No such file or directory: 'dq_offer_desc_bad_pkey_vw.sql'
  </code></pre>
  
  <p>The 'dq_offer_desc_bad_pkey_vw.sql' file does not contain 'gen_dts' in it.</p>
  
  <p>I appreciate the help in advance.</p>