简体   繁体   English

Python 提取/解压 RAR 文件错误

[英]Python extract / unrar RAR file errors

I'm trying to extract RAR file in Python script.我正在尝试在 Python 脚本中提取 RAR 文件。 I've found only two possible ways to do that: by using patoolib or by using rarfile .我发现只有两种可能的方法来做到这一点:使用 patolib 或使用rarfile Unfortunately, both of those options raise a lot of errors in my code, and I have no idea how to fix this.不幸的是,这两个选项都会在我的代码中引发很多错误,我不知道如何解决这个问题。

First, I've tried only patool and patoolib.首先,我只尝试了 patool 和 patolib。 After the errors I've switched to rarfile and unrar.出现错误后,我切换到 rarfile 和 unrar。 The first one seems to be easier, but I don't understand the error.第一个似乎更容易,但我不明白错误。 The second one requires a lot of actions in environmental variables and I am not sure if I've done it right.第二个需要在环境变量中执行很多操作,我不确定我是否做得对。

import patoolib
patoolib.extract_archive("my_file.rar", outdir=r"C:\Users\User1\Desktop\Example_dir")

The error says:错误说:

if verbosity >= 0:
TypeError: '>=' not supported between instances of 'str' and 'int'

This option I get from here .我从这里得到的这个选项。 I know that this error says something about string variable, but I don't know how to interpret it.我知道这个错误说明了一些关于字符串变量的内容,但我不知道如何解释它。

The second option was to use rarfile and unrar.第二种选择是使用 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")

This option throws a vexatious error:这个选项会抛出一个令人讨厌的错误:

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),

Also, there was another error:此外,还有一个错误:

RarCannotExec: Unrar not installed? (rarfile.UNRAR_TOOL='unrar')

The rarfile documentation says, that UNRAR_TOOL needs to be the path to unrar.exe. rarfile文档说,UNRAR_TOOL 需要是 unrar.exe 的路径。 I've done "pip install unrar", I have installed all of the libraries from above by "pip".我已经完成了“pip install unrar”,我已经通过“pip”安装了上面的所有库。 Basing on this answer, I've downloaded UnRARDLL ( http://www.rarlab.com/rar/UnRARDLL.exe ), but I don't know what .exe file should I assign to UNRAR_TOOL.基于这个答案,我已经下载了 UnRARDLL ( http://www.rarlab.com/rar/UnRARDLL.exe ),但我不知道我应该将什么 .exe 文件分配给 UNRAR_TOOL。 I've added environment path to C:\\Program Files (x86)\\UnrarDLL\\x64\\UnRAR64.dll as UNRAR_LIB_PATH but it didn't help.我已将环境路径添加到 C:\\Program Files (x86)\\UnrarDLL\\x64\\UnRAR64.dll 作为 UNRAR_LIB_PATH 但它没有帮助。

I just want to unrar some files by Python script.我只想通过 Python 脚本解压缩一些文件。 The easier, the better.越容易越好。 Can You tell me what am I doing wrong?你能告诉我我做错了什么吗? Maybe there is another way to unrar some files?也许有另一种方法来解压缩某些文件?

The TypeError exception claims you were trying to compare a string and integer. TypeError异常声称您试图比较字符串和整数。 If the reference to if verbosity >= 0: is correct, that would imply that the verbosity variable is a string.如果对if verbosity >= 0:的引用是正确的,则意味着verbosity变量是一个字符串。
Perhaps you set verbosity = '1' instead of verbosity = 1 previously.也许您之前设置了verbosity = '1'而不是verbosity = 1

The other error says just what it says: 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) 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)
The code expects to find the executable of either one of rar , unrar or 7z (7zip).该代码期望找到rarunrar7z (7zip) 之一的可执行文件。 If you have them installed, perhaps you need to tell patoolib about them, somehow.如果您安装了它们,也许您需要以某种方式告诉patoolib有关它们的信息。

The line线

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

is looking okay, but if it doesn't work, you might have to follow the steps in your linked answer and set the UNRAR_LIB_PATH environment variable instead.看起来不错,但如果它不起作用,您可能必须按照链接答案中的步骤操作并设置UNRAR_LIB_PATH环境变量。
This should explain how you can set environment variables on Windows: https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/这应该解释如何在 Windows 上设置环境变量: https : //helpdeskgeek.com/windows-10/add-windows-path-environment-variable/

A few moments ago I found the master source to patoolib .不久前,我找到了 patolib 的主 Now I know, that the arguments for patoolib.extract_archive function are: patoolib.extract_archive(archive, verbosity, outdir, program, format, compression) .现在我知道, patoolib.extract_archive函数的参数是: patoolib.extract_archive(archive, verbosity, outdir, program, format, compression) I don't know what verbosity does in this function.我不知道verbosity在这个函数中做了什么。 Also, how should I set program ?另外,我应该如何设置program For now I have something like this:现在我有这样的事情:

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)

The error says:错误说:

SyntaxError: EOL while scanning string literal

You need to go to https://www.rarlab.com/rar_add.htm download UnRAR for Windows - Command line freeware Windows UnRAR, extract it to a folder and add:您需要到https://www.rarlab.com/rar_add.htm下载 UnRAR for Windows - 命令行免费软件 Windows UnRAR,将其解压到一个文件夹并添加:

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

because the rarfile isn't looking for a .dll, is looking for an executable .exe因为 rarfile 不是在寻找 .dll,而是在寻找可执行文件 .exe

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

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