标签:学习 def 实施 str list div sep col port
一、先将压缩文件解压至指定目录
import zipfile import os zip_src = "目标指定压缩文件" def unzip_file(): r = zipfile.ZipFile(zip_src) if r: fz = zipfile.ZipFile(zip_src, ‘r‘) for file in fz.namelist(): fz.extract(file, ‘解压到指定目录‘) else: print(‘This is not zip‘) #print(unzip_file())
二、再对解压内容进行压缩打包并实施改名,格式依然为zip格式
# 压缩 #打包目录为zip文件(未压缩) def make_zip(source_dir, output_filename): zipf = zipfile.ZipFile(output_filename, ‘w‘) pre_len = len(os.path.dirname(source_dir)) for parent, dirnames, filenames in os.walk(source_dir): for filename in filenames: pathfile = os.path.join(parent, filename) arcname = pathfile[pre_len:].strip(os.path.sep) #相对路径 zipf.write(pathfile, arcname) zipf.close() abc = make_zip("指定目标解压内容",‘压缩成文件,并实施改名‘) print(abc)
标签:学习 def 实施 str list div sep col port
原文地址:https://www.cnblogs.com/LT-XILI/p/14343447.html