linux解压zip文件命令_Python解压zip文件的例子

更新时间:2019-05-05    来源:python    手机版     字体:

【www.bbyears.com--python】

 代码如下


#!/usr/bin/python
#-*-coding:cp936 -*-
import os
import zipfile

def unzip(infile,targetDir):
    if not os.path.exists(infile):
        return False
    if not os.path.isdir(targetDir):
        os.makedirs(targetDir)
    try:
        zipObj = zipfile.ZipFile(infile)
        files = zipObj.namelist()
        for name in files:
            if name[-1] == "/": # is a directory
                if not os.path.isdir(os.path.join(targetDir,name)): # not exists
                    os.makedirs(os.path.join(targetDir,name))
            else:
                f = open(os.path.join(targetDir,name),"wb")
                f.write(zipObj.read(name))
                f.close()
        zipObj.close()
    except Exception as e:
        print e
        return False

if __name__ == "__main__":
    currDir = os.path.dirname(os.path.abspath(__file__))
    unzip("test.zip",currDir)

本文来源:http://www.bbyears.com/jiaocheng/49922.html

猜你感兴趣

热门标签

更多>>

本类排行