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?