python 文件夹压缩

import os
import zipfile

def zipDir(dirpath,outFullName):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    :param outFullName: 压缩文件保存路径+xxxx.zip
    """
    zip = zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED)
    for path,dirnames,filenames in os.walk(dirpath):
        fpath = path.replace(dirpath,'')
        for filename in filenames:
            print('ziping %s' % filename)
            zip.write(os.path.join(path,filename),os.path.join(fpath,filename))
    zip.close()

def argument():
    content = input('Input target foler and zip Name,like: "a b"\n')
    tar,des = content.split()
    return tar,des

if __name__ == '__main__':
    target,des = argument()
    zipDir(target,'%s.zip'% des)
posted @ 2019-12-09 10:38  wztshine  阅读(349)  评论(0编辑  收藏  举报