Not managing to extract RAR archive using rarfile module

陌路散爱 提交于 2019-12-25 04:46:34

问题


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 rarfile2.8 and unrar0.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.


回答1:


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.




回答2:


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)



回答3:


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)



回答4:


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)


来源:https://stackoverflow.com/questions/40251033/not-managing-to-extract-rar-archive-using-rarfile-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!