繁体   English   中英

从 Google colab 笔记本中提取 Google Drive zip

[英]Extract Google Drive zip from Google colab notebook

我已经在谷歌驱动器上有一个(2K 图像)数据集的压缩包。 我必须在 ML 训练算法中使用它。 下面的代码以字符串格式提取内容:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import io
import zipfile
# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Download a file based on its file ID.
#
# A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
file_id = '1T80o3Jh3tHPO7hI5FBxcX-jFnxEuUE9K' #-- Updated File ID for my zip
downloaded = drive.CreateFile({'id': file_id})
#print('Downloaded content "{}"'.format(downloaded.GetContentString(encoding='cp862')))

但是我必须将其提取并存储在一个单独的目录中,因为它更容易处理(以及理解)数据集。

我试图进一步提取它,但得到“不是 zipfile 错误”

dataset = io.BytesIO(downloaded.encode('cp862'))
zip_ref = zipfile.ZipFile(dataset, "r")
zip_ref.extractall()
zip_ref.close()

谷歌云端硬盘数据集

注意:数据集仅供参考,我已经将这个 zip 下载到我的 google 驱动器中,我指的是驱动器中的文件。

你可以简单地使用这个

!unzip file_location

要将文件解压缩到目录:

!unzip path_to_file.zip -d path_to_directory

要从 Google colab 笔记本中提取 Google Drive zip:

import zipfile
from google.colab import drive

drive.mount('/content/drive/')

zip_ref = zipfile.ZipFile("/content/drive/My Drive/ML/DataSet.zip", 'r')
zip_ref.extractall("/tmp")
zip_ref.close()

Colab 研究团队有一个笔记本可以帮助您。

不过,简而言之,如果您正在处理一个 zip 文件,就像我一样,它主要是数千张图像,我想将它们存储在驱动器内的一个文件夹中,然后执行此操作 -

!unzip -u "/content/drive/My Drive/folder/example.zip" -d "/content/drive/My Drive/folder/NewFolder"

-u部分仅在新/必要时控制提取。 如果您突然失去连接或硬件关闭,这一点很重要。

-d创建目录并将提取的文件存储在那里。

当然,在执行此操作之前,您需要安装驱动器

from google.colab import drive 
drive.mount('/content/drive')

我希望这有帮助! 干杯!!

首先,在 colab 上安装 unzip:

!apt install unzip

然后使用 unzip 解压缩您的文件:

!unzip  source.zip -d destination.zip

安装 GDrive:

from google.colab import drive
drive.mount('/content/gdrive')

打开链接 -> 复制授权码 -> 将其粘贴到提示符中,然后按“Enter”

检查 GDrive 访问:

!ls "/content/gdrive/My Drive"

从 GDrive 解压缩(q 代表“安静”)文件:

!unzip -q "/content/gdrive/My Drive/dataset.zip"

首先新建一个目录:

!mkdir file_destination

现在,是时候用解压后的文件来扩充目录了:

!unzip file_location -d file_destination

对于 Python

连接到驱动器,

from google.colab import drive
drive.mount('/content/drive')

检查目录

!ls!pwd

用于解压

!unzip drive/"My Drive"/images.zip

在驱动器上安装后,使用shutil.unpack_archive 它适用于几乎所有的存档格式(例如,“zip”、“tar”、“gztar”、“bztar”、“xztar”),而且很简单:

import shutil
shutil.unpack_archive("filename", "path_to_extract")

请在 google colab 中使用此命令

解压要解压的文件,然后解压位置

!unzip "drive/My Drive/Project/yourfilename.zip" -d "drive/My Drive/Project/yourfolder"

而不是GetContentString() ,而是使用 GetContentFile() 。 它将保存文件而不是返回字符串。

downloaded.GetContentFile('images.zip') 

然后您可以稍后使用unzip压缩它。

简单的连接方式

1)您必须验证身份验证

from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()

2)融合谷歌驱动器

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse

3) 验证凭据

import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

4)创建一个驱动器名称以在colab('gdrive')中使用它并检查它是否工作

!mkdir gdrive
!google-drive-ocamlfuse gdrive
!ls gdrive
!cd gdrive

尝试这个:

!unpack file.zip

如果它现在工作或文件是 7z 试试下面

!apt-get install p7zip-full
!p7zip -d file_name.tar.7z
!tar -xvf file_name.tar

或者

!pip install pyunpack
!pip install patool

from pyunpack import Archive
Archive(‘file_name.tar.7z’).extractall(‘path/to/’)
!tar -xvf file_name.tar

我们假设您已经将您的 googleDrive 安装到 googleColab。 如果您只想提取包含 .csv 扩展名的 zip 文件。 只需调用 pandas 属性 read_csv

pd.read_csv('/content/drive/My Drive/folder/example.zip')

如何在 Google Colab 中提取带有密码的 zip 文件? 我尝试了很多不同的事情,但没有任何运气。

这对我有用。

!apt install unzip

然后我使用此代码解压缩文件

!unzip /content/file.zip -d /content/

如果不先在 Colab 上安装unzip ,您将始终收到错误消息。

在我的想法中,您必须走一条特定的道路,例如:

从 google.colab 导入驱动器 drive.mount('/content/drive/') cd drive/MyDrive/f/

然后 :

!apt install unzip !unzip zip_folder.zip -d unzip_folder在此处输入图像描述

暂无
暂无

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

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