首页 > 编程技术 > python

Python批量解压&压缩文件夹的示例代码

发布时间:2022-4-4 19:26 作者:用余生去守护

一、python批量解压

提示:如果是重要数据解压前请先备份,解压后会覆盖原压缩文件!!

解压前:

解压后:文件名为英文:

文件名中包含中文:

代码如下

import os
import shutil
import zipfile
 
# 首先引入需要的工具包
# shutil为后期移动文件所需,可以忽略此项 
# 路径改这里!
#parent_path = r'输入路径,会解压该路径下的所有zip压缩文件'
parent_path = r'E:\py\python3.7\test\test99\zip' 
# 文件类型选择
# 可以自行更改压缩文件类型,需要引入其它工具包,如tarfile等
# 这里是因为在自己的windows上,zip比较常见,其他类型请自行更改
file_flag = '.zip'   #修改需解压的格式 例如:.rar
# 删除已解压的zip文件
# 不建议初次使用,在确定程序无误后可以添加使用
def del_old_zip(file_path):
    os.remove(file_path)  
# 解压
def decompress(file_path, root):
    # 开始
    # zipfile打开zip文件
    z = zipfile.ZipFile(f'{file_path}', 'r') 
    # 解压
    z.extractall(path=f"{root}")    # path为解压路径,解包后位于该路径下 
    # 判断是否需要重复解包
    for names in z.namelist():
        if names.endswith(file_flag):
            z.close()
            return 1 
    # 结束
    z.close() 
    return 0
     
# 因为我在使用过程中发现有些zip解包后会混在一起
# 在平时大家手动解压时可能也会遇到提示是否覆盖的问题
# 下面的两个函数解决这一问题 
# 开始要先创建一个大文件夹  与压缩包名字相同
# 避免后期混乱和麻烦
def start_dir_make(root, dirname):
    os.chdir(root)
    os.mkdir(dirname)
    return os.path.join(root, dirname) 
# 去除多余文件夹
def rem_dir_extra(root, father_dir_name):
    # 递归要注意信息的正常处理  搞不好上一个调用已经改变了东西  而下面的调用还是使用之前的数据 
    try: 
       # 判断文件夹重名  开始
        for item in os.listdir(os.path.join(root, father_dir_name)): 
            # 第一步判断是不是一个文件夹,如果不是则跳过本次循环
            if not os.path.isdir(os.path.join(root, father_dir_name, item)):
                continue 
            # 判断是否要脱掉一层目录结构
            # 文件夹名字要相同,且子目录中只有单独的一个文件夹
            if item == father_dir_name and len(
                    os.listdir(os.path.join(root, father_dir_name))) == 1: 
                # 改变工作目录
                os.chdir(root)                 
                # 将无用文件夹重命名,因为直接移动会有重名错误
                os.rename(father_dir_name, father_dir_name + '-old')                 
                # 移动文件后删除空文件夹
                shutil.move(os.path.join(root, father_dir_name + '-old', item), os.path.join(root))
                os.rmdir(os.path.join(root, father_dir_name + '-old')) 
                # 将去掉一层目录结构后的文件夹继续作为父本递归处理下去
                # 这里要注意,上面已经发生过数据的改动,所以下面递归传参一定要正确!
                rem_dir_extra(root, item) 
            else: 
                # 处理那些不满足上面条件的文件夹
                rem_dir_extra(os.path.join(root, father_dir_name), item) 
    except Exception as e: 
        # 打印错误信息
        print("清除文件夹出错" + str(e)) 
# 入口
if __name__ == '__main__': 
    flag = 1 
    while flag: 
        #  循环遍历文件夹
        for root, dirs, files in os.walk(parent_path): 
            # 读取文件名
            for name in files:
                if name.endswith(file_flag): 
                    # 创建文件夹
                    new_ws = start_dir_make(root, name.replace(file_flag, '')) 
                    # zip文件地址
                    zip_path = os.path.join(root, name) 
                    # 解压
                    flag = decompress(zip_path, new_ws) 
                    # 删除解压后的文件
                    # 有点危险
                    # 但不删除又可能会重复运行
                     # 一定要备份或先测试,不然可能会凉,自己选择修改
                    del_old_zip(zip_path) 
                    # 去掉多余的文件结构
                    rem_dir_extra(root, name.replace(file_flag, '')) 
                    print(f'{root}\\{name}'.join(['文件:', '\n解压完成\n']))
  
    # 由于解压可能解了好几次 所以可能会有已经解压好的父级目录重名无法处理 这里要再处理一次
    rem_dir_extra(os.path.split(parent_path)[0], os.path.split(parent_path)[1])  
    print("解压完成啦,记得检查有没有zip格式之外的呀!\n\n其他格式需要自己改一下了")

二、python批量压缩

压缩前:

压缩后:

代码如下:

import zipfile
import os

def zip_yasuo(start_dir):
    file_news = start_dir + '.zip'
    if not os.path.isfile(file_news):
        z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED)
        for dir_path, dir_names, file_names in os.walk(start_dir):
            file_path = dir_path.replace(start_dir, '')
            file_path = file_path and file_path + os.sep or ''
            for filename in file_names:
                z.write(os.path.join(dir_path, filename), file_path+filename)
        z.close()

if __name__ == "__main__":
    base_path = r"E:\py\python3.7\test\test99\zip"
    base_path_list = os.listdir(base_path)
    for base_path_list_one in base_path_list:
        base_path_list_one_dir = os.path.join(base_path,base_path_list_one)
        # 子目录
        print("准备压缩需要压缩的子目录", base_path_list_one_dir)
        if os.path.isdir(base_path_list_one_dir):
            zip_yasuo(base_path_list_one_dir)

到此这篇关于Python批量解压&压缩文件夹的示例代码的文章就介绍到这了,更多相关Python解压 压缩文件夹内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://blog.csdn.net/qq_45365214/article/details/123914055

标签:[!--infotagslink--]

您可能感兴趣的文章: