maze88 maze88 - 1 month ago 16
Python Question

Not managing to extract RAR archive using rarfile module

I have been trying to make a script that extracts *.rar files for but am receiving errors. I've been struggling to understand the documentation of the module to no avail (I'm new to programming so sometimes get a bit lost in all the docs).

Here is the relevant part of my code, and the error received.

Snippet from my code:

import rarfile

def unrar(file):


Error received:

File "", line 26, in unrar
AttributeError: 'str' object has no attribute 'extract_all'

I have installed
2.8 and
0.3 using
(note sure if the later was necessary).

Thanks in advance for any assistance correcting my function or helping understand the package's documentation.


Support for RAR files in general is quite poor, this experience is par for the course.

In order to get the rarfile Python module to work, you have to also install a supported tool for extracting RAR files. Your only two choices are bsdtar or unrar. Do not install these with Pip, you have to install these with your Linux package manager (or install them yourself, if you think that the computer's time is more valuable than your time). For example on Debian-based systems (this includes Ubuntu) run,

sudo apt install bsdtar


sudo apt install unrar

Note that bsdtar does not have the same level of support for RAR files that Unrar does. Some newer RAR files will not extract with bsdtar.

Then your code should look something like this:

import rarfile

def unrar(file):
    rf = rarfile.RarFile(file)


Note the use of rf.extract_all(), not rf.rarfile.extract_all().

If you are just doing extract_all then there is no need to use a rarfile module, though. You can just use the subprocess module:

import subprocess
path = '/path/to/archive.rar'
subprocess.check_call(['unrar', 'x', path])

The rarfile module is basically nothing more than a wrapper around subprocess anyway.

Of course, if you have a choice in the matter, I recommend migrating your archives to a more portable and better supported archive format.