python批量压缩解压文件

张开发
2026/4/8 11:29:04 15 分钟阅读

分享文章

python批量压缩解压文件
importosimportzipfilefrompathlibimportPath# # 1. 批量解压所有 zip 文件# defbatch_unzip(zip_folder,save_folder): 批量解压文件夹里所有 .zip 文件 zip_folder: 存放压缩包的文件夹 save_folder: 解压到哪里 # 创建输出文件夹Path(save_folder).mkdir(exist_okTrue)# 遍历所有文件forfileinPath(zip_folder).glob(*.zip):# 解压目录名 原压缩包名字out_pathos.path.join(save_folder,file.stem)print(f正在解压{file.name})withzipfile.ZipFile(file,r)aszf:zf.extractall(out_path)print(✅ 全部解压完成)# # 2. 批量压缩文件/文件夹# defbatch_zip(source_folder,save_folder): 批量压缩每个文件/文件夹单独打成一个zip source_folder: 要压缩的东西所在文件夹 save_folder: 压缩包保存位置 Path(save_folder).mkdir(exist_okTrue)# 遍历所有文件和文件夹foriteminPath(source_folder).iterdir():zip_nameos.path.join(save_folder,item.name.zip)print(f正在压缩{item.name})withzipfile.ZipFile(zip_name,w,zipfile.ZIP_DEFLATED)aszf:# 如果是文件夹递归加文件ifitem.is_dir():forfile_pathinitem.rglob(*):zf.write(file_path,arcnamefile_path.relative_to(item.parent))# 如果是文件直接加else:zf.write(item,arcnameitem.name)print(✅ 全部压缩完成)# # 使用示例改路径就能跑# if__name____main__:# 解压示例batch_unzip(zip_folderrC:\Users\Administrator\Downloads\带合并,save_folderrC:\Users\Administrator\Downloads\带合并\解压缩)# 压缩示例# batch_zip(# source_folderrC:\Users\Administrator\Downloads\3,# save_folderrC:\Users\Administrator\Downloads\3-# )遍历文件defget_files_path(path):# path os.curdirpath_list[]forroot,folders,filesinos.walk(path):# print(root,folders,files)forfileinfiles:new_pathroot\\filepath_list.append(new_path)new_pathreturnpath_listdefget_excel_all_sheetnames(file_path): 查看sheetname df_dictpd.read_excel(file_path,sheet_nameNone)print(df_dict.keys())# print(df_dict.values())defre_name(path_list,save_path): 重命名文件,并复制到新地方 forold_pathinpath_list:new_nameold_path.split(\\)[-2]_old_path.split(\\)[-1]new_pathsave_path\\new_nameprint(old_path,new_path)try:# 重命名并移动文件shutil.copy(old_path,new_path)print(f文件{old_path}已成功重命名并移动到{new_path})exceptFileNotFoundError:print(f文件{old_path}未找到。)exceptPermissionError:print(没有足够的权限进行操作。)全部代码importosimportzipfilefrompathlibimportPathimportshutilimportpandasaspd# # 1. 批量解压所有 zip 文件# defbatch_unzip(zip_folder,save_folder): 批量解压文件夹里所有 .zip 文件 zip_folder: 存放压缩包的文件夹 save_folder: 解压到哪里 # 创建输出文件夹Path(save_folder).mkdir(exist_okTrue)# 遍历所有文件forfileinPath(zip_folder).glob(*.zip):# 解压目录名 原压缩包名字out_pathos.path.join(save_folder,file.stem)print(f正在解压{file.name})withzipfile.ZipFile(file,r)aszf:zf.extractall(out_path)print(✅ 全部解压完成)# # 2. 批量压缩文件/文件夹# defbatch_zip(source_folder,save_folder): 批量压缩每个文件/文件夹单独打成一个zip source_folder: 要压缩的东西所在文件夹 save_folder: 压缩包保存位置 Path(save_folder).mkdir(exist_okTrue)# 遍历所有文件和文件夹foriteminPath(source_folder).iterdir():zip_nameos.path.join(save_folder,item.name.zip)print(f正在压缩{item.name})withzipfile.ZipFile(zip_name,w,zipfile.ZIP_DEFLATED)aszf:# 如果是文件夹递归加文件ifitem.is_dir():forfile_pathinitem.rglob(*):zf.write(file_path,arcnamefile_path.relative_to(item.parent))# 如果是文件直接加else:zf.write(item,arcnameitem.name)print(✅ 全部压缩完成)# 遍历文件defget_files_path(path):# path os.curdirpath_list[]forroot,folders,filesinos.walk(path):# print(root,folders,files)forfileinfiles:new_pathroot\\filepath_list.append(new_path)new_pathreturnpath_listdefget_excel_all_sheetnames(file_path): 查看sheetname df_dictpd.read_excel(file_path,sheet_nameNone)print(df_dict.keys())# print(df_dict.values())defre_name(path_list,save_path): 重命名文件,并复制到新地方 forold_pathinpath_list:new_nameold_path.split(\\)[-2]_old_path.split(\\)[-1]new_pathsave_path\\new_nameprint(old_path,new_path)try:# 重命名并移动文件shutil.copy(old_path,new_path)print(f文件{old_path}已成功重命名并移动到{new_path})exceptFileNotFoundError:print(f文件{old_path}未找到。)exceptPermissionError:print(没有足够的权限进行操作。)defdeal_excel_file(file_path,sheet_name原始记录): excel表单逻辑处理函数 dfpd.read_excel(file_path,sheet_namesheet_name,headerNone)tjrqdf.iloc[0,0]bbscsjdf.iloc[1,0]df.columnsdf.iloc[2]dfdf.loc[3:,].reset_index()df[统计日期]tjrq df[报表生成时间]bbscsj df[源自]file_path df[表]sheet_namereturndfif__name____main__:plget_files_path(rD:\data\mydata\批量解压缩)foriinpl:re_name({i},rD:\data\mydata\批量改名后)print(i)zpget_files_path(rD:\data\mydata\批量改名后)df_list[]foriinzp:dfdeal_excel_file(i)df_list.append(df)print(i)finall_dfpd.concat(df_list)

更多文章