2014-11-05 182 views
0

我想解壓縮* .sec.gz這是一個zip文件的文件。但我事先得到BADFILE .....有人能指導來解決這個.....文件存在的文件夾中類型的* .SEC ........感謝解壓縮文件

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') 

輸出:

BadZipfile        Traceback (most recent call last) 
<ipython-input-7-5134b63e752e> in <module>() 
    27  zfile.close() 
    28 
---> 29 k=unzip('C://test//08October2014//DATA_INTV_NEW//Oct0814//1.sec.gz') 

<ipython-input-7-5134b63e752e> in unzip(path) 
    13 
    14 def unzip(path): 
---> 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': 
--> 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: 
--> 811    raise BadZipfile, "File is not a zip file" 
    812   if self.debug > 1: 
    813    print endrec 

BadZipfile: File is not a zip file 
+0

http://stackoverflow.com/questions/20762094/how-are-zlib-gzip-and-zip-related-what-are-is-common-and-how-are-they-differen – 2014-11-05 09:38:50

回答

3

錯誤消息是完全準確的:這不是一個zip文件。這是一個gzip文件,完全不同。你應該使用gzip module

+0

Thanks for ur指導,能夠使用gzip.open()檢索 – 2014-11-05 12:15:49