Reputation: 940
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
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.rarfile.extract_all()
unrar(rarpath)
Error received:
File "unrarer.py", line 26, in unrar
rf.rarfile.extract_all()
AttributeError: 'str' object has no attribute 'extract_all'
I have installed rarfile
2.8 and unrar
0.3 using pip
(note sure if the later was necessary).
Thanks in advance for any assistance correcting my function or helping understand the package's documentation.
Upvotes: 2
Views: 12413
Reputation: 441
I had the same issue. rarfile
uses the unrar
library dependence. It is able to uncompress the most of .rar files but not all of them. So, an option to install unrar
can work in some cases.
sudo apt-get install unrar
Otherwise you can download the rar
library, but it's not free. There was an advice of @tiago-gomes to install user's version of rar
, but nobody can guarantee if it is safe and reliable: https://www.rarlab.com/rar_add.htm
You can use trial version of that as a temporary solution or just buy a license: https://www.rarlab.com/download.htm Then add a link to archiver in your Python code:
rarfile.UNRAR_TOOL=os.path.join("<path_to_unrar_folder>", "unrar")
Upvotes: 0
Reputation: 160
if you are in windows, it worked for me. You need to go to https://www.rarlab.com/rar_add.htm download UnRAR for Windows - Command line freeware Windows UnRAR, execute it, extract it to a folder and add the executable path in your code after importing rarfile:
rarfile.UNRAR_TOOL = r"C:\FilePath\UnRAR.exe"
Upvotes: 1
Reputation: 483
try this
import fnmatch
from rarfile import RarFile
path = r'C:\Users\byqpz\Desktop\movies\rars'
destinationPath = r'C:\Users\byqpz\Desktop\movies\destination'
for root, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, '*.rar'):
fullPath = os.path.join(root, filename)
RarFile(fullPath).extract(destinationPath)
Upvotes: 0
Reputation: 213837
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
Or,
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 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.
Upvotes: 2
Reputation: 77407
rf.rarfile
is the name of the file which you can see by printing its value. Remove that and check out help(rarfile.RarFile)
for the method you want.
import rarfile
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.extractall()
unrar(rarpath)
Upvotes: 0