简体   繁体   中英

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

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.

Try using rf.extractall() instead of rf.rarfile.extract_all() because rf.rarfile is the name of the file as @tdelaney pointed out.

and change import unrar to from unrar import rarfile because you need unrar on top of rarfile to open/read rar files and extract them into memory

from unrar import rarfile
rarpath='/home/maze/Desktop/test.rar'

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

unrar(rarpath)

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)

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)

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM