3

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?

KS0232
  • 157
  • 2
  • 9
  • Don't add an answer to your question, if it isn't an answer. Edit your question instead. The `SyntaxError: EOL while scanning string literal` is because you forgot a closing `"` after `program=r"`, before your closing bracket `)`. – nyov Sep 14 '19 at 12:58

3 Answers3

2

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).
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/

nyov
  • 1,382
  • 7
  • 23
  • I have not set verbosity anywhere in my code, to any value. – KS0232 Sep 14 '19 at 07:32
  • Also, I've set the `UNRAR_LIB_PATH` environment variable, same as in your link and same as in my link. The error is still the same. – KS0232 Sep 14 '19 at 07:53
1

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=r"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
Baris Senyerli
  • 642
  • 7
  • 13
KS0232
  • 157
  • 2
  • 9
1

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

Tiago Gomes
  • 161
  • 1
  • 9