python 压缩文件夹

import os
import zipfile

def zipDir(dirpath):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    """
    outFullName = dirpath + '.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()


if __name__ == '__main__':
    target = input('Input target foler: \n')
    zipDir(target)
posted @ 2020-02-29 14:34  wztshine  阅读(374)  评论(0编辑  收藏  举报