无法使用 rarfile 模块提取 RAR 存档
Not managing to extract RAR archive using rarfile module
我一直在尝试制作一个脚本来提取 *.rar 文件,但收到错误。我一直在努力理解模块的文档,但无济于事(我是编程新手,所以有时会在所有文档中迷失方向)。
这是我的代码的相关部分,以及收到的错误。
我的代码片段:
import rarfile
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.rarfile.extract_all()
unrar(rarpath)
收到错误:
File "unrarer.py", line 26, in unrar
rf.rarfile.extract_all()
AttributeError: 'str' object has no attribute 'extract_all'
我已经使用 pip
安装了 rarfile
2.8 和 unrar
0.3(请注意是否需要后者)。
在此先感谢您提供任何帮助来更正我的功能或帮助理解包的文档。
rf.rarfile
是您可以通过打印其值看到的文件的名称。删除它并检查 help(rarfile.RarFile)
您想要的方法。
import rarfile
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.extractall()
unrar(rarpath)
对 RAR 文件的支持总体上很差,这种体验是可以接受的。
为了让 rarfile
Python 模块工作,您还必须安装一个支持的工具来提取 RAR 文件。您仅有的两个选择是 bsdtar
或 unrar
。不要用 Pip 安装它们,你必须用你的 Linux 包管理器安装它们(或者你自己安装它们,如果你认为计算机的时间比你的时间更宝贵)。例如在基于 Debian 的系统上(这包括 Ubuntu)运行、
sudo apt install bsdtar
或者,
sudo apt install unrar
请注意,bsdtar 对 RAR 文件的支持级别与 Unrar 不同。一些较新的 RAR 文件无法使用 bsdtar 提取。
那么您的代码应该如下所示:
import rarfile
def unrar(file):
rf = rarfile.RarFile(file)
rf.extract_all()
unrar('/home/maze/Desktop/test.rar')
注意使用 rf.extract_all()
,而不是 rf.rarfile.extract_all()
。
如果你只是做 extract_all
那么就没有必要使用 rarfile
模块。您可以只使用 subprocess
模块:
import subprocess
path = '/path/to/archive.rar'
subprocess.check_call(['unrar', 'x', path])
无论如何,rarfile
模块基本上只不过是 subprocess
的包装器。
当然,如果您在这件事上有选择,我建议您将您的档案迁移到一种更便携、更好支持的档案格式。
试试这个
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)
如果你在 windows,它对我有用。您需要去 https://www.rarlab.com/rar_add.htm 下载 UnRAR for Windows - 命令行免费软件 Windows UnRAR,执行它,将其解压缩到一个文件夹,并在导入 rar 文件后在您的代码中添加可执行路径:
rarfile.UNRAR_TOOL = r"C:\FilePath\UnRAR.exe"
我一直在尝试制作一个脚本来提取 *.rar 文件,但收到错误。我一直在努力理解模块的文档,但无济于事(我是编程新手,所以有时会在所有文档中迷失方向)。
这是我的代码的相关部分,以及收到的错误。
我的代码片段:
import rarfile
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.rarfile.extract_all()
unrar(rarpath)
收到错误:
File "unrarer.py", line 26, in unrar
rf.rarfile.extract_all()
AttributeError: 'str' object has no attribute 'extract_all'
我已经使用 pip
安装了 rarfile
2.8 和 unrar
0.3(请注意是否需要后者)。
在此先感谢您提供任何帮助来更正我的功能或帮助理解包的文档。
rf.rarfile
是您可以通过打印其值看到的文件的名称。删除它并检查 help(rarfile.RarFile)
您想要的方法。
import rarfile
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.extractall()
unrar(rarpath)
对 RAR 文件的支持总体上很差,这种体验是可以接受的。
为了让 rarfile
Python 模块工作,您还必须安装一个支持的工具来提取 RAR 文件。您仅有的两个选择是 bsdtar
或 unrar
。不要用 Pip 安装它们,你必须用你的 Linux 包管理器安装它们(或者你自己安装它们,如果你认为计算机的时间比你的时间更宝贵)。例如在基于 Debian 的系统上(这包括 Ubuntu)运行、
sudo apt install bsdtar
或者,
sudo apt install unrar
请注意,bsdtar 对 RAR 文件的支持级别与 Unrar 不同。一些较新的 RAR 文件无法使用 bsdtar 提取。
那么您的代码应该如下所示:
import rarfile
def unrar(file):
rf = rarfile.RarFile(file)
rf.extract_all()
unrar('/home/maze/Desktop/test.rar')
注意使用 rf.extract_all()
,而不是 rf.rarfile.extract_all()
。
如果你只是做 extract_all
那么就没有必要使用 rarfile
模块。您可以只使用 subprocess
模块:
import subprocess
path = '/path/to/archive.rar'
subprocess.check_call(['unrar', 'x', path])
无论如何,rarfile
模块基本上只不过是 subprocess
的包装器。
当然,如果您在这件事上有选择,我建议您将您的档案迁移到一种更便携、更好支持的档案格式。
试试这个
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)
如果你在 windows,它对我有用。您需要去 https://www.rarlab.com/rar_add.htm 下载 UnRAR for Windows - 命令行免费软件 Windows UnRAR,执行它,将其解压缩到一个文件夹,并在导入 rar 文件后在您的代码中添加可执行路径:
rarfile.UNRAR_TOOL = r"C:\FilePath\UnRAR.exe"