Python批量解压.zip,7z文件

import zipfile
import py7zr
import os

def uncompress(path_name):
    suffix = path_name.rsplit('.', 1)[1]
    if suffix == 'zip':
        if zipfile.is_zipfile(path_name):
            try:
                with zipfile.ZipFile(path_name) as zip_f:
                    zip_f.extractall(path_name.rsplit(".zip")[0])
            except Exception as e:
                print('Error when uncompress file! info: ', e)
                return False
            else:
                return True
        else:
            print('This is not a true zip file!')
            return False
    if suffix == '7z':
        if py7zr.is_7zfile(path_name):
            try:
#d_name为特殊处理的密码,文件名字的一部分 d_name
= path_name.split(" ")[1].split(".")[0] with py7zr.SevenZipFile(path_name,password=d_name, mode='r') as sevenZ_f: sevenZ_f.extractall(path_name.rsplit(".7z")[0]) except Exception as e: print('Error when uncompress file! info: ', e) return False else: return True else: print('This is not a true 7z file!') return False # 压缩包来源目录 if __name__ == '__main__': folder_name = input("请输入文件夹--例如--E:\\workspace\\unzipTest:") os.chdir(folder_name) files = os.listdir(folder_name) for f in files: f_path = folder_name + os.sep + f if os.path.isfile(f_path): print("解压--"+f) uncompress(path_name=f_path)

 引用

--https://www.pythonf.cn/read/143728

原文地址:https://www.cnblogs.com/onexixi/p/14260172.html