【发布时间】:2017-09-17 13:36:17
【问题描述】:
我有一个包含 2 个文件的 RAR 存档,我只想提取一个。我在另一个答案中发现我可以使用rarfile 包,根据documentation 包含extract 函数。但是,当我尝试运行脚本时,我会得到一个 FileNotFoundError: [WinError 2] 和以下信息:During handling of the above exception, another exception occurred: ... rarfile.RarCannotExec: Unrar not installed? (rarfile.UNRAR_TOOL='unrar')。
根据我能找到的信息,我发现这可能与 PATH 中缺少 Unrar.exe 可执行文件有关,我尝试添加它,但没有任何改变。另一个建议是将rarfile.UNRAR_TOOL='unrar' 添加到脚本中,作为配置包行为的一种方式,同样的错误。
这是我的 MWE,用 Python 3.5.3 编写和测试:
from rarfile import RarFile
with RarFile('Test.rar') as file:
file.extract(file.namelist()[0])
文件正在正确打开,因为file.namelist() 返回存档的内容。
提前致谢!
【问题讨论】:
-
哪个版本的python?
-
Python 3.5.3。我会更新 OP。