我有一个包含 2 个文件的 RAR 存档,但我只想提取一个。我在另一个答案中发现我可以使用该rarfile包,根据文档,该包包含该extract函数。但是,当我尝试运行脚本时,我得到FileNotFoundError: [WinError 2]以下信息:During handling of the above exception, another exception occurred: ... rarfile.RarCannotExec: Unrar not installed? (rarfile.UNRAR_TOOL='unrar')。
从我找到的信息来看,我发现这可能与Unrar.exePATH 中缺少可执行文件有关,我尝试添加它,但没有任何改变。另一个建议是添加rarfile.UNRAR_TOOL='unrar'到脚本中作为配置包行为的一种方式,同样的错误。
这是我的 MWE,用 Python 3.5.3 编写和测试:
from rarfile import RarFile
with RarFile('Test.rar') as file:
file.extract(file.namelist()[0])
Run Code Online (Sandbox Code Playgroud)
文件正在正确打开,因为file.namelist()返回了存档的内容。
提前致谢!