繁体   English   中英

Python 提取/解压 RAR 文件错误

[英]Python extract / unrar RAR file errors

我正在尝试在 Python 脚本中提取 RAR 文件。 我发现只有两种可能的方法来做到这一点:使用 patolib 或使用rarfile 不幸的是,这两个选项都会在我的代码中引发很多错误,我不知道如何解决这个问题。

首先,我只尝试了 patool 和 patolib。 出现错误后,我切换到 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) 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"

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

不久前,我找到了 patolib 的主 现在我知道, 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=or"C:\Users\User1\Desktop\Example_dir", program=r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll)

错误说:

SyntaxError: EOL while scanning string literal

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM