python将目录下及子目录下所有的压缩包解压到特定目录

你可以使用Python内置的os和zipfile模块来完成这个任务。下面是一个示例代码:

import os
import zipfile


# 定义一个函数来解压.zip文件
def unzip_file(zip_file_path, save_path):
    with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
        zip_ref.extractall(save_path)

# 定义一个函数来遍历目录并解压.zip文件
def unzip_all_zip_files(directory_path, save_path):
    """
    intro:
        find all the files end with .zip and unzip these files to the `save_path`.
        
    args:
        :param str directory_path: find all .zip in this path.
        :param str save_path: store the files in this path.
    
    return:
        void.
    """
    for root, dirs, files in os.walk(directory_path):
        for file in files:
            if file.endswith('.zip'):
                # 解压文件到当前文件夹
                unzip_file(os.path.join(root, file), save_path)

if __name__=="__main__":
    # 使用示例
    path = "/Volumes/ymz/_paper_with_code_and_data/3_microDL/data/S-BIAD25/mouse/_retardance"
    save_path = "/Volumes/ymz/_paper_with_code_and_data/3_microDL/data/S-BIAD25/mouse/_retardance/_all"
    unzip_all_zip_files(path, save_path)

这个代码会遍历指定的目录(包括子目录)中的所有文件,如果文件名以.zip结尾,则调用unzip_file函数将其解压到当前文件夹。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值