title: How to loop through the list of .tar.gz files using linux command in python
body: <p>Using python 2.7</p>
  
  <p>I have a list of *.tat.gz files on a linux box. Using python, I want to loop through the files and extract those files in a different location, under their respective folders. </p>
  
  <blockquote>
    <p>For example: if my file name is ~/TargetData/zip/1440198002317590001.tar.gz<br>
    then I want to untar and ungzip this file in a different location under its 
    respective folder name i.e.  ~/TargetData/unzip/1440198002317590001. </p>
  </blockquote>
  
  <p>I have written some code but I am not able to loop through the files. In a command line I am able to untar using $ tar -czf 1440198002317590001.tar.gz 1440198002317590001 command. But I want to be able to loop through the .tar.gz files. The code is mentioned below. Here, I’m not able to loop just the files Or print only the files. Can you please help?</p>
  
  <pre><code>    import os
      inF = []
      inF = str(os.system('ls ~/TargetData/zip/*.tar.gz'))
      #print(inF)
      if inF is not None:
          for files in inF[:-1]:
          print files
      """
      os.system('tar -czf files /unzip/files[:-7]')
      # This is what i am expecting here files = "1440198002317590001.tar.gz" and files[:-7]= "1440198002317590001"
      """
  </code></pre>
  
  <p>Have you ever worked on this type of use case? Your help is greatly appreciated!! Thank you!</p>