【问题标题】:Python extract / unrar RAR file errorsPython 提取/解压缩 RAR 文件错误
【发布时间】:2020-01-15 17:42:47
【问题描述】:

我正在尝试在 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'

我从here 得到这个选项。我知道这个错误说明了字符串变量,但我不知道如何解释它。

第二个选项是使用 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 documentation 说,UNRAR_TOOL 需要是 unrar.exe 的路径。我已经完成了“pip install unrar”,我已经通过“pip”从上面安装了所有库。根据this 的回答,我已经下载了 UnRARDLL (http://www.rarlab.com/rar/UnRARDLL.exe),但我不知道应该将哪个 .exe 文件分配给 UNRAR_TOOL。我已将环境路径添加到 C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll 作为 UNRAR_LIB_PATH 但它没有帮助。

我只想通过 Python 脚本解压缩一些文件。越容易越好。你能告诉我我做错了什么吗?也许还有另一种解压缩文件的方法?

【问题讨论】:

  • 如果不是答案,请不要为您的问题添加答案。改为编辑您的问题。 SyntaxError: EOL while scanning string literal 是因为您忘记了在 program=r" 之后的右括号 ) 之前的右括号 "

标签: python extract rar unrar


【解决方案1】:

TypeError 异常声称您试图比较字符串和整数。如果对if verbosity >= 0: 的引用是正确的,则意味着verbosity 变量是一个字符串。
也许您之前设置了verbosity = '1' 而不是verbosity = 1

另一个错误就是它所说的:could not find an executable program to extract format rar; candidates are (rar,unrar,7z)
该代码期望找到rarunrar7z (7zip) 之一的可执行文件。如果您安装了它们,也许您需要以某种方式告诉patoolib

线

rarfile.UNRAR_TOOL = r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll"

看起来不错,但如果它不起作用,您可能必须按照 linked answer 中的步骤设置 UNRAR_LIB_PATH 环境变量。
这应该解释了如何在 Windows 上设置环境变量:https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/

【讨论】:

  • 我没有在我的代码中的任何地方设置详细程度,任何值。
  • 另外,我设置了UNRAR_LIB_PATH 环境变量,与您的链接和我的链接相同。错误还是一样。
【解决方案2】:

不久前我找到了大师 source 到 patoolib 。现在我知道,patoolib.extract_archive 函数的参数是:patoolib.extract_archive(archive, verbosity, outdir, program, format, compression)。我不知道verbosity 在这个函数中做了什么。另外,我应该如何设置program?现在我有这样的东西:

import patoolib
patoolib.extract_archive(archive="my_file.rar", verbosity=0 ,outdir=r"C:\Users\User1\Desktop\Example_dir", program=r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll)

错误提示:

SyntaxError: EOL while scanning string literal

【讨论】:

    【解决方案3】:

    你需要去https://www.rarlab.com/rar_add.htm下载UnRAR for Windows - 命令行免费软件Windows UnRAR,解压到一个文件夹并添加:

    rarfile.UNRAR_TOOL = r"C:\YourPath\UnRAR.exe"
    

    因为 rarfile 不是在寻找 .dll,而是在寻找可执行的 .exe

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 2011-12-26
      • 2013-05-12
      • 1970-01-01
      • 2021-01-15
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多