title: To unzip a file
body: <p>I want to unzip a file of type *.sec.gz which is a zipfile. But i'm getting badfile.....Can someone guide to resolve this.....File present in the folder is of type *.sec ........Thanks in advance</p>
  
  <pre><code>import zipfile
  def unzip(path):
      zfile = zipfile.ZipFile(path)
      for name in zfile.namelist():
          (dirname, filename) = os.path.split(name)
          if filename == '':
              # directory
              if not os.path.exists(dirname):
                  os.mkdir(dirname)
          else:
              # file
              fd = open(name, 'w')
              fd.write(zfile.read(name))
              fd.close()
      zfile.close()
  
  k=unzip('C://test//08October2014//DATA_INTV_NEW//Oct0814//1.sec.gz')
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>BadZipfile                                Traceback (most recent call last)
  &lt;ipython-input-7-5134b63e752e&gt; in &lt;module&gt;()
       27     zfile.close()
       28 
  ---&gt; 29 k=unzip('C://test//08October2014//DATA_INTV_NEW//Oct0814//1.sec.gz')
  
  &lt;ipython-input-7-5134b63e752e&gt; in unzip(path)
       13 
       14 def unzip(path):
  ---&gt; 15     zfile = zipfile.ZipFile(path)
       16     for name in zfile.namelist():
       17         (dirname, filename) = os.path.split(name)
  
  C:\Python27\Lib\zipfile.pyc in __init__(self, file, mode, compression, allowZip64)
      768         try:
      769             if key == 'r':
  --&gt; 770                 self._RealGetContents()
      771             elif key == 'w':
      772                 # set the modified flag so central directory gets written
  
  C:\Python27\Lib\zipfile.pyc in _RealGetContents(self)
      809             raise BadZipfile("File is not a zip file")
      810         if not endrec:
  --&gt; 811             raise BadZipfile, "File is not a zip file"
      812         if self.debug &gt; 1:
      813             print endrec
  
  BadZipfile: File is not a zip file
  </code></pre>