【问题标题】:How to extract rar files inside google colab如何在google colab中提取rar文件
【发布时间】:2018-10-23 06:25:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: python ubuntu google-colaboratory rar


    【解决方案1】:

    以下代码 sn-p 为我工作:

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

    【讨论】:

      【解决方案2】:

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

      注意:要使此代码正常工作,您需要在 colab 中安装一个名为 rarfile 的模块。可以通过以下代码sn-p来实现:

      pip install rarfile
      

      在不深入了解它的工作原理的情况下,继续将下面的代码 sn-p 复制到 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 是您要提取它的位置。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          试试这个:

          pip install patool
          import patoolib
          patoolib.extract_archive("foo_bar.rar", outdir="path here")
          

          【讨论】:

            【解决方案5】:
            !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')
            

            查看这段代码 sn-p

            真的很简单,超级快

            【讨论】:

              【解决方案6】:

              以下代码 sn-p 对我有用:

              !pip install unrar
              !unrar x file_path
              

              【讨论】:

                【解决方案7】:
                !unrar x "{Complete path to rar file}"
                

                这对我有用

                【讨论】:

                • 我认为不需要在双引号内给出路径。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-11-09
                • 1970-01-01
                • 2021-01-22
                • 2021-07-02
                • 1970-01-01
                • 2020-06-27
                相关资源
                最近更新 更多