[python] Batch decompress all compressed packages (rar, zip, 7z) under the folder

        When the folder function contains many compressed packages, it is time-consuming and laborious to decompress, especially when there are nested folders, it is even more troublesome to decompress. Franpper today brings you a method to recursively traverse all files and folders under the specified path, and decompress all compressed packages in batches, helping you to decompress with one click.

        Common compression package formats include rar, zip, and 7z. Franpper writes the decompression methods of these types of files in the method. Let’s take 7z as an example to introduce it in detail. See the bottom of the complete code.

Table of contents

1. Code introduction

2. Matters needing attention

3. Complete code


1. Code introduction

        The first is the function mkdir function, which is used to create a new folder to store decompressed files.

def mkdir(path):
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print(path + '创建成功')
    else:
        print(path + '目录已存在')

         Generate the unzip_log.txt log file to record the path of the file that failed to decompress, and these files need to be manually decompressed.

 wrong_log = os.path.join(folder_path, 'unzip_log.txt')

         When recursively traversing folders, get the names of all folders in the folder. If the name of the compressed package is the same as the name of the folder in the same directory (if it exists), it is considered to have been decompressed and will not be decompressed.

contents = os.listdir(root)
folders = [folder for folder in contents if os.path.isdir(os.path.join(root, folder))]

         For the file to be decompressed, get its name and generate a folder.

 zipFile_name = file.split('.')[0:-1]
 zipFile_name = '.'.join(zipFile_name)

         Next, perform the decompression operation:

with py7zr.SevenZipFile(zipFile_path, mode='r') as z:
    z.extractall(path=unzip_zipFile_path)

        The path of the file that failed to decompress is recorded in the log:

with open(wrong_log, 'a') as f:
    f.write(f'\n {zipFile_path}')

 2. Matters needing attention

        have to be aware of is:

        1) When using rarfile to decompress the rar file, the decompression will fail. You need to copy UnRAR.exe in the winrar directory to the python script directory. As shown below:

        2) When using zipfile to compress the zip file, there will be garbled characters in the decompressed file. You need to modify the two places in the zipfile.py file as shown in the figure below.

3. Complete code


import os
import zipfile
import rarfile
import py7zr
"""
解压文件
"""


def mkdir(path):
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print(path + '创建成功')
    else:
        print(path + '目录已存在')


def unzipFile(folder_path):
    wrong_log = os.path.join(folder_path, 'unzip_log.txt')
    for root, dirs, files in os.walk(folder_path):
        contents = os.listdir(root)
        folders = [folder for folder in contents if os.path.isdir(os.path.join(root, folder))]  # 该目录下文件夹名称列表
        for file in files:
            if file.endswith('7z'):
                zipFile_name = file.split('.')[0:-1]
                zipFile_name = '.'.join(zipFile_name)
                if zipFile_name in folders:
                    continue
                # 没有重名文件则进行解压
                else:
                    # 创建解压文件夹路径
                    unzip_zipFile_path = os.path.join(root, zipFile_name)
                    mkdir(unzip_zipFile_path)
                    zipFile_path = os.path.join(root, file)
                    print(zipFile_path)
                    try:
                        with py7zr.SevenZipFile(zipFile_path, mode='r') as z:
                            z.extractall(path=unzip_zipFile_path)
                    except:
                        with open(wrong_log, 'a') as f:
                            f.write(f'\n {zipFile_path}')

            elif file.endswith('rar'):  # file 是待解压文件
                # 有重名文件说明被解压过,跳过
                rarFile_name = file.split('.')[0:-1]
                rarFile_name = '.'.join(rarFile_name)
                if rarFile_name in folders:
                    continue
                # 没有重名文件则进行解压
                else:
                    # 创建解压文件夹路径
                    unzip_rarFile_path = os.path.join(root, rarFile_name)
                    mkdir(unzip_rarFile_path)
                    rarFile_path = os.path.join(root, file)
                    print(rarFile_path)
                    try:
                        with rarfile.RarFile(rarFile_path) as rar_ref:
                            rar_ref.extractall(unzip_rarFile_path)
                    except:
                        pass
                        with open(wrong_log, 'a') as f:
                            f.write(f'\n {rarFile_path}')

            elif file.endswith('zip'):  # file 是待解压文件
                # 有重名文件说明被解压过,跳过
                rarFile_name = file.split('.')[0:-1]
                rarFile_name = '.'.join(rarFile_name)
                if rarFile_name in folders:
                    continue
                # 没有重名文件则进行解压
                else:
                    # 创建解压文件夹路径
                    unzip_rarFile_path = os.path.join(root, rarFile_name)
                    mkdir(unzip_rarFile_path)
                    rarFile_path = os.path.join(root, file)
                    print(rarFile_path)
                    try:
                        with zipfile.ZipFile(rarFile_path, 'r') as zip_ref:
                            zip_ref.extractall(unzip_rarFile_path)
                    except:
                        with open(wrong_log, 'a') as f:
                            f.write(f'\n {rarFile_path}')
            else:
                continue


unzipFile(r"G:\work\")

Guess you like

Origin blog.csdn.net/weixin_58283091/article/details/130988374