赞
踩
py7zr 是一个开源库,详细的说明请参阅Github:py7zr
代码简单示范压缩:(解压的方法类似,可参考github上的示例)
import os import py7zr import time #获取py文件所在路径 dir = os.path.dirname(os.path.realpath('__file__')) #系统时间 now_date = time.strftime("%Y-%m-%d") #压缩档名称 archive_name = now_date+"_MarketData.7z" archive = py7zr.SevenZipFile(archive_name, 'w') #要压缩的文件夹路径 data_file = dir+"\\data" if os.path.exists(data_file): for dir_path, dir_names, file_names in os.walk(data_file): for filename in file_names: print(filename) fpath = dir_path.replace(data_file, '') file_path = os.path.join(dir_path, filename) filename = os.path.join(fpath, filename) archive.write(file_path, arcname=filename) archive.close() print("压缩完成!") else: print("文件不存在。")
代码参考了 python使用py7zr做加密压缩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。