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:
File "unrarer.py", line 26, in unrar
AttributeError: 'str' object has no attribute 'extract_all'
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
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) rf.extract_all() unrar('/home/maze/Desktop/test.rar')
Note the use of
If you are just doing
extract_all then there is no need to use a
rarfile module, though. You can just use the
import subprocess path = '/path/to/archive.rar' subprocess.check_call(['unrar', 'x', path])
rarfile module is basically nothing more than a wrapper around
Of course, if you have a choice in the matter, I recommend migrating your archives to a more portable and better supported archive format.