编程语言
首页 > 编程语言> > Python 压缩文件

Python 压缩文件

作者:互联网


def zipDir(dirpath, outFullName):
"""
压缩指定文件夹
:param dirpath: 目标文件夹路径
:param outFullName: 压缩文件保存路径+xxxx.zip
:return: 无
"""
# time.sleep(50)
zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
for path, dirnames, filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath, '')

for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
print('压缩成功!')

标签:zip,Python,压缩文件,文件夹,dirpath,path,filename,os
来源: https://www.cnblogs.com/chenxiyuxiao/p/11724010.html