python - Python 提取/解压缩 RAR 文件错误
问题描述
我正在尝试在 Python 脚本中提取 RAR 文件。我发现只有两种可能的方法:使用 patoolib 或使用rarfile。不幸的是,这两个选项都会在我的代码中引发很多错误,我不知道如何解决这个问题。
首先,我只尝试了 patool 和 patoolib。出现错误后,我切换到 rarfile 和 unrar。第一个似乎更容易,但我不明白错误。第二个需要对环境变量进行大量操作,我不确定我是否做得对。
import patoolib
patoolib.extract_archive("my_file.rar", outdir=r"C:\Users\User1\Desktop\Example_dir")
错误说:
if verbosity >= 0:
TypeError: '>=' not supported between instances of 'str' and 'int'
我从这里得到的这个选项。我知道这个错误说明了字符串变量,但我不知道如何解释它。
第二个选项是使用 rarfile 和 unrar。
import patoolib
from unrar import rarfile
from pyunpack import Archive
rarfile.UNRAR_TOOL = r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll"
rarpath = 'my_file.rar'
rf = rarfile.RarFile(rarpath)
rf.extractall()
rf.extractall(r"C:\Users\User1\Desktop\Example_dir")
这个选项会抛出一个无理取闹的错误:
PatoolError('patool can not unpack\n' + str(p.stderr)) pyunpack.PatoolError: patool can not unpack patool error: error extracting G:\program\test.rar: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),
此外,还有另一个错误:
RarCannotExec: Unrar not installed? (rarfile.UNRAR_TOOL='unrar')
rarfile文档说,UNRAR_TOOL 需要是 unrar.exe 的路径。我已经完成了“pip install unrar”,我已经通过“pip”从上面安装了所有库。基于这个答案,我下载了 UnRARDLL ( http://www.rarlab.com/rar/UnRARDLL.exe ),但我不知道应该将哪个 .exe 文件分配给 UNRAR_TOOL。我已将环境路径添加到 C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll 作为 UNRAR_LIB_PATH 但它没有帮助。
我只想通过 Python 脚本解压缩一些文件。越容易越好。你能告诉我我做错了什么吗?也许还有另一种解压缩文件的方法?
解决方案
TypeError
异常声称您试图比较字符串和整数。如果对的引用if verbosity >= 0:
是正确的,那将意味着该verbosity
变量是一个字符串。
也许你设置verbosity = '1'
而不是verbosity = 1
以前。
另一个错误说明了它所说的内容:could not find an executable program to extract format rar; candidates are (rar,unrar,7z)
. 代码期望找到,或(7zip)
之一的可执行文件。如果您安装了它们,也许您需要以某种方式告诉它们。rar
unrar
7z
patoolib
线
rarfile.UNRAR_TOOL = r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll"
看起来不错,但如果它不起作用,您可能必须按照链接答案中的步骤设置UNRAR_LIB_PATH
环境变量。
这应该解释如何在 Windows 上设置环境变量:https ://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/
推荐阅读
- regex - 如何在 Python 中找到一个包含(和不)环绕某个模式的字符串?
- reactjs - React Fullcalendar v4 工具提示
- r - 使用R&dplyr总结——group_by、count、mean、sd
- scala - scala中的变量字符串插值
- npm - npm -install 错误,我无法安装 appium
- javascript - 如何为处理数组的javascripts循环制作流程图
- mysql - JOIN 查询需要很长时间并创建问题“将 HEAP 转换为 MyISAM
- php - 从 Gravity Forms API 检查值
- php - 如何使用 MongoDb 特定运算符 $size 查找数组大小大于、小于、介于和不等于 Laravel / jenssegers 中的值?
- dashboard - 有没有办法在单独的仪表板上显示 Google Cloud Build Data?