python压缩文件为zip格式

import os
import zipfile
def compressFolder(folderPath, compressPathName):
    '''
    :param folderPath: 文件夹路径
    :param compressPathName: 压缩包路径
    :return:
    '''
    zip = zipfile.ZipFile(compressPathName, 'w', zipfile.ZIP_DEFLATED)

    for path, dirNames, fileNames in os.walk(folderPath):
        fpath = path.replace(folderPath, '')
        for name in fileNames:
            fullName = os.path.join(path, name)

            name = fpath + '\\' + name
            zip.write(fullName, name)

    zip.close()


compressFolder('E:/hydra', 'E:/hydra.zip')

猜你喜欢

转载自blog.csdn.net/qq_34237321/article/details/108009859