代码效果演示 Gitee源码
import zipfile
from os import path,walk,chdir
def zipFiles(pathsList):
for i,r in enumerate(pathsList,1):
if path.isfile(r):
fDir,fName=path.split(r)
fPre,fSuf=path.splitext(fName)
chdir(fDir)
zipPath=fDir+'\\'+fPre+'.zip'
zipObj = zipfile.ZipFile(zipPath, 'w', zipfile.ZIP_DEFLATED)
zipObj.write(fName)
zipObj.close()
else:
chdir(r)
fDir,fName=path.split(r)
fPre,fSuf=path.splitext(fName)
zipPath=fDir+'\\'+fName+'.zip'
zipObj = zipfile.ZipFile(zipPath, 'w', zipfile.ZIP_DEFLATED)
for p, dirs, files in walk(r):
for name in files:
zPath=path.join(p, name).replace(r+'\\','')
zipObj.write(zPath)
for name in dirs:
zPath=path.join(p, name).replace(r+'\\','')
zipObj.write(zPath)
zipObj.close()
print('第{}个文件夹压缩完毕,储存路径{}'.format(i,zipPath))
pathsList=[
r'D:\[download]\forTest\folder_for_test\FFT1',
r'D:\[download]\forTest\folder_for_test',r'D:\[download]\forTest\folder_for_test\新建 Microsoft Excel 工作表.xlsx']
zipFiles(pathsList)
|