简体   繁体   中英

Python extract / unrar RAR file errors

I'm trying to extract RAR file in Python script. I've found only two possible ways to do that: by using patoolib or by using 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. After the errors I've switched to rarfile and 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.

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. I've done "pip install unrar", I have installed all of the libraries from above by "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. I've added environment path to C:\\Program Files (x86)\\UnrarDLL\\x64\\UnRAR64.dll as UNRAR_LIB_PATH but it didn't help.

I just want to unrar some files by Python script. 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. If the reference to if verbosity >= 0: is correct, that would imply that the verbosity variable is a string.
Perhaps you set verbosity = '1' instead of verbosity = 1 previously.

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) .
The code expects to find the executable of either one of rar , unrar or 7z (7zip). If you have them installed, perhaps you need to tell patoolib about them, somehow.

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.
This should explain how you can set environment variables on Windows: https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/

A few moments ago I found the master source to patoolib . Now I know, that the arguments for patoolib.extract_archive function are: patoolib.extract_archive(archive, verbosity, outdir, program, format, compression) . I don't know what verbosity does in this function. Also, how should I set 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:

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

because the rarfile isn't looking for a .dll, is looking for an executable .exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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