如何提取 google colab 中的 rar 文件

How to extract rar files inside google colab

我在 google 驱动器 中有一个 数据集,我想在 google colab 中使用它。但是我无法 解压 rar 文件 任何 means.So 到目前为止,我已经尝试安装 python 库以及 ubuntu 包,如 "unrar ,rar ,unrar-free ,unar ,unp"我只是无法打开该死的 file.Here 是每个命令的结果:

!rar x data.rar

RAR 5.40   Copyright (c) 1993-2016 Alexander Roshal   15 Aug 2016
Trial version             Type RAR -? for help


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4

!unrar data.rar

UNRAR 5.50 freeware      Copyright (c) 1993-2017 Alexander Roshal


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4

!unp 元data.rar

RAR 5.40   Copyright (c) 1993-2016 Alexander Roshal   15 Aug 2016
Trial version             Type RAR -? for help


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4

UNRAR 5.50 freeware      Copyright (c) 1993-2017 Alexander Roshal


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4
Can't exec "file": No such file or directory at /usr/bin/unp line 419.
Failed to detect file type of meta-data.rar.
WARNING: There were errors while processing files!

None 其他人正在重新工作,欢迎提出任何想法。

以下代码片段对我有用:

get_ipython().system_raw("unrar x file_name")

您可以编写一个简单的 python 代码,从 google colab 中直接在您的 google 驱动器中提取 zip 文件。

注意:要使此代码正常工作,您需要在 colab 中安装名为 rarfile 的模块。您可以通过以下代码片段执行此操作:

pip install rarfile

无需深入了解其工作原理,直接将下面的代码片段复制到 google colab 和 运行 单元格中。

def unrar(dpath,xpath):
  for rar in os.listdir(dpath):
    filepath = os.path.join(dpath, rar)
    with rarfile.RarFile(filepath) as opened_rar:
      for f in opened_rar.infolist():
        print (f.filename, f.file_size)
        opened_rar.extractall(xpath)

unrar(dpath,xpath)

这里,dpath是你的.rar文件所在的路径目录。 xpath 是你要解压的地方。

从那以后我尝试了很多解决方案,但最好的方法是使用 "rsync" Linux 命令将文件从驱动器获取到协作的存储(使用 "!apt install rsync 安装 rsync ") 然后 "unzip" 命令。之后速度快如闪电 (71.32MB/s)。

试试这个:

pip install patool
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")
!pip install pyunpack
!pip install patool
from pyunpack import Archive
Archive('Location of the rar file').extractall('Location where you want to have the folder')

查看此代码片段

真的很简单,超级快

以下代码片段对我有用:

!pip install unrar
!unrar x file_path
!unrar x "{Complete path to rar file}"

这对我有用