繁体   English   中英

解压缩文件

[英]To unzip a file

我想解压缩* .sec.gz类型的文件,这是一个zipfile。 但是我得到了坏文件.....有人可以解决这个问题吗.....文件夹中存在的文件类型为* .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

错误消息是完全正确的:这不是一个zip文件。 这是一个gzip文件,完全不同。 您应该使用gzip模块

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM