title: I want to extract a .tgz file and extract any subdirectories that have files that are .tgz and .tar
body: <p>I'm using the code below to extract <code>.tgz</code> files. The type of log files (<code>.tgz</code>) that I need to extract have sub-directories that have other <code>.tgz</code> files and <code>.tar</code> files inside them. I want to extract those too.</p>
  
  <p>Ultimately, I'm trying to search for certain strings in all <code>.log</code> files and <code>.txt</code> files that may appear in a <code>.tgz</code> file. </p>
  
  <p>Below is the code that I'm using to extract the <code>.tgz</code> file. I've been trying to work out how to extract the sub-files (<code>.tgz</code> and <code>.tar</code>). So far, I've been unsuccessful.</p>
  
  <pre><code>import os, sys, tarfile
  
  try:
      tar = tarfile.open(sys.argv[1] + '.tgz', 'r:gz')
      for item in tar:
          tar.extract(item)
      print 'Done.'
  except:
      name = os.path.basename(sys.argv[0])
      print name[:name.rfind('.')], '&lt;filename&gt;'
  </code></pre>