繁体   English   中英

如何在google colab中提取rar文件

[英]How to extract rar files inside google colab

在 google drive 中有一个数据集,我想在google colab中使用。但我无法通过任何方式解压缩rar 文件。到目前为止,我已经尝试安装 python 库以及 ubuntu 包,如“unrar,rar,unrar-free ,unar,unp”,我无法打开该死的文件。以下是每个命令的结果:

.rar x 数据.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 数据.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元数据.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!

其他人都没有重新工作,所以欢迎任何想法。

以下代码片段对我有用:

!pip install unrar
!unrar x file_path

以下代码片段对我有用:

get_ipython().system_raw("unrar x file_name")
!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')

看看这个代码片段

真的很简单而且超级快

您可以编写一个简单的 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是您要提取它的位置。

尝试这个:

pip install patool
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")
!unrar x "{Complete path to rar file}"

这对我有用

下面的代码将起作用

!unrar x dataset/Hnd.rar

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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