使用python将zip批量解压

介绍
1)将.zip文件批量解压
2)将解压文件合并到一个文件夹下


程序实现功能

使用python将zip批量解压,解压之后并对解压后的文件批量处理,将解压后的文件扁平化并重命名,即将解压后的文件树扁平为一个文件,文件中存放整个文件树中的文件。


import zipfile
import os
import sys

a = r"C:\Users\Desktop\tar"  #zipfile 的路径
b = r"C:\Users\Desktop\123"   #解压到路径unzip下
c = r"C:\Users\Desktop\fgh"   #最终解压路径
old_folder_name = ""



# 将zip文件解压处理,并放到指定的文件夹里面去
# def iter_folder(file, zip_file_name, destination_path, path):
def iter_folder(zip_file_path, file_name , final_path):
    path = os.path.join(zip_file_path, file_name)
    dirs = os.listdir(path)
    if dirs is []: 
        return 
    for name in dirs:
        if os.path.isdir(os.path.join(path, name)):
            iter_folder(path, name, final_path)
        else:
            # try:
            new_name = '{0}_{1}'.format(old_folder_name, name)
            print(new_name)
            (filepath, tempfilename) = os.path.split(new_name)
            # 若文件名过长,需要裁短
            if len(tempfilename) > 60:
                old_folder_name = old_folder_name[0:(59-len(name))]
            
            string = 'rename "{0}" "{0}_{1}"'.format(os.path.join(path, name), old_folder_name, name)
            os.system(string)
            # print(string)
            new_name = '{0}_{1}'.format(old_folder_name, name)
            string = 'copy "{0}" "{1}"'.format(os.path.join(path, new_name), final_path)
            os.system(string)
            # print(string)

# handle_folder(b,c)
#将解压后的文件处理
def handle_folder(destination_path,final_path):
    for name in os.listdir(destination_path):
        # if not os.path.exists(os.path.join(final_path, name)):
        #     final_path = os.path.join(final_path, name)
        #     os.mkdir(final_path)
        global old_folder_name
        old_folder_name = name
        iter_folder(destination_path, name, final_path)
        # final_path = os.path.dirname(final_path)

    

def unzip_file(zip_file_name,destination_path):
    archive = zipfile.ZipFile(zip_file_name,mode='r')
    for file in archive.namelist():
        archive.extract(file, destination_path)
    # print(archive.namelist())

        

def zipfile_name(file_dir):
    # 读取文件夹下面的文件名.zip
    L=[]
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.zip':    # 读取带zip 文件
                L.append(os.path.join(root, file)) #将目录和文件名合成一个路径
                # print(L)

    return L

#入口函数
def main():
    if not os.path.exists(b):
        os.mkdir(b)
    if not os.path.exists(c):
        os.mkdir(c)
    fn=zipfile_name(a)
    # print(fn)
    for file in fn:
        (filepath, tempfilename) = os.path.split(file)
        (filename, extension) = os.path.splitext(tempfilename)
        sub_b = os.path.join(b, filename)
        if not os.path.exists(sub_b):
            os.mkdir(sub_b)
        # print(sub_b)
        unzip_file(file,sub_b)
    handle_folder(b,c)
    os.remove(b)  #用于删除指定路径的文件
    
        
if __name__ == "__main__":
    main()
    print("done")



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值