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: ') zipDir(target)