Python 批量解压

import os
import shutil
import rarfile
import zipfile
import re


def main(target_path):
    for root, dirs, files in os.walk(target_path):
        now_dir = root # 解压后的存放目录
        for file in files:
            path = os.path.join(now_dir, file)
            path_out = os.path.join(now_dir, 'down', file.split('.')[0])
            if '.rar' in file:
                rf = rarfile.RarFile(path) # 待解压文件
                rf.extractall(path_out) # 解压指定文件路径
                print(file,":OK")
            if '.zip' in file:
                z = zipfile.ZipFile(path)
                z.extractall(path_out) # path为解压路径,解包后位于该路径下
                print(file,":OK")


if __name__ == '__main__':
    target_path = "dongtai/"
    main(target_path)

你可能感兴趣的:(python学习,批量,解压缩,python)