PatoolError Python:解压缩.rar文件时出现问题

问题描述

我无法在Python中使用patoolib解压缩.rar文件。 我总是收到错误消息:

PatoolError:找不到可执行程序来提取rar格式;候选人是(rar,unrar,7z),

我正在使用macOS,带有Spyder IDE的Anaconda Navigator,并且已经安装了unrar软件包。我不明白为什么patool找不到可执行程序,而我却无法解决这个问题。

有人可以告诉我如何解决吗?

编辑:

调用方法时出现错误,该方法定义为解压缩.zip和.rar文件

def unzip_file(path_to_file,out_dir):
    # Extracts path_to_file in out_dir
    if path_to_file[-3:] == 'zip':
        with zipfile.ZipFile(path_to_file,'r') as zip_ref:
            zip_ref.extractall(out_dir)
            return
    if path_to_file[-3:] == 'rar':
        patoolib.extract_archive(path_to_file,outdir=out_dir)    
        return
    
    raise FileNotFoundError(path_to_file)

解决方法

如果您使用的是macOS,则应该安装Homebrew(如果尚未安装),然后运行brew install 7z,然后重试。

我猜测您使用的这个工具并没有进行实际的归档,它只知道如何与那里的各种存档程序对话,因此您需要安装一个可以读写{ {1}}存档。