title: How do I list contents of a gz file without extracting it in python?
body: <p>I have a <code>.gz</code> file and I need to get the name of files inside it using python.</p>
  
  <p>This question is the same as <a href="https://stackoverflow.com/questions/19666983/how-do-i-list-contents-of-a-gzip-file-without-extracting-it-in-python">this one</a> </p>
  
  <p>The only difference is that my file is <code>.gz</code> not <code>.tar.gz</code> so the <code>tarfile</code> library did not help me here</p>
  
  <p>I am using <code>requests</code> library to request a URL. The response is a compressed file.</p>
  
  <p>Here is the code I am using to download the file</p>
  
  <pre><code>response = requests.get(line.rstrip(), stream=True)
          if response.status_code == 200:
              with open(str(base_output_dir)+"/"+str(current_dir)+"/"+str(count)+".gz", 'wb') as out_file:
                  shutil.copyfileobj(response.raw, out_file)
              del response
  </code></pre>
  
  <p>This code downloads the file with name <code>1.gz</code> for example. Now if I opened the file with an archive manger the file will contain something like <code>my_latest_data.json</code> </p>
  
  <p>I need to extract the file and the output be <code>my_latest_data.json</code>. </p>
  
  <p>Here is the code I am using to extract the file</p>
  
  <pre><code>inF = gzip.open(f, 'rb')
  outfilename = f.split(".")[0]
  outF = open(outfilename, 'wb')
  outF.write(inF.read())
  inF.close()
  outF.close()
  </code></pre>
  
  <p>The <code>outputfilename</code> variable is a string I provide in the script but I need the real file name (<code>my_latest_data.json</code>)</p>