使用python解压rar文件

Ubuntu中,首先安装unrar

sudo apt install unrar

安装完后,使用

which unrar

会得到安装位置

/usr/bin/unrar

==================

还需要安装rarfile

pip3 install rarfile

================

from rarfile import RarFile

RarFile.UNRAR_TOOL='/usr/bin/unrar' #这行可以不要,但是这个unrar一定要安装,不然会报错

RarFile("待解压rar文件路径").extractall("要解压的位置")  >>> 比如:RarFile("zzz.rar").extractall("./01") >>意思是:把与这个python script在同一directory的zzz.rar解压到同目录的中01directory中

上面的script也可以写成函数,比如:

from rarfile import RarFile

def un_rar(rar_filepath, unpack_filepath):

  RarFile(rar_filepath).extractall(unpack_filepath)

#调用函数

un_rar("/home/<username>/Desktop/zzz.rar", "/home/<username>/Desktop/01/")

参考文件:

https://rarfile.readthedocs.io/en/latest/api.html

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

猜你喜欢

转载自www.cnblogs.com/profesor/p/12727286.html