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

Lax*_*ant 8 python machine-learning zipfile google-drive-api google-colaboratory

我已经在谷歌硬盘上有一个(2K图像)数据集的拉链.我必须在ML训练算法中使用它.Code下面以字符串格式提取内容:

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')))
Run Code Online (Sandbox Code Playgroud)

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

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

dataset = io.BytesIO(downloaded.encode('cp862'))
zip_ref = zipfile.ZipFile(dataset, "r")
zip_ref.extractall()
zip_ref.close()
Run Code Online (Sandbox Code Playgroud)

Google云端硬盘数据集

注意:数据集仅供参考,我已经将此zip文件下载到我的google驱动器中,而且我只是指我的驱动器中的文件.

gia*_*pnh 44

要将文件解压缩到目录:

!unzip path_to_file.zip -d path_to_directory
Run Code Online (Sandbox Code Playgroud)


小智 16

你可以简单地使用它

!unzip file_location
Run Code Online (Sandbox Code Playgroud)


小智 9

首先,在colab上安装unzip:

!apt install unzip
Run Code Online (Sandbox Code Playgroud)

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

!unzip  source.zip -d destination.zip
Run Code Online (Sandbox Code Playgroud)


Suv*_*uvo 9

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')
Run Code Online (Sandbox Code Playgroud)

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


小智 9

请在 google colab 中使用此命令

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

!unzip "drive/My Drive/Project/yourfilename.zip" -d "drive/My Drive/Project/yourfolder"
Run Code Online (Sandbox Code Playgroud)


Alo*_*ian 7

要从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()
Run Code Online (Sandbox Code Playgroud)


Plo*_*oon 6

安装 GDrive:

from google.colab import drive
drive.mount('/content/gdrive')
Run Code Online (Sandbox Code Playgroud)

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

检查 GDrive 访问:

!ls "/content/gdrive/My Drive"
Run Code Online (Sandbox Code Playgroud)

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

!unzip -q "/content/gdrive/My Drive/dataset.zip"
Run Code Online (Sandbox Code Playgroud)


sar*_*pta 6

首先新建一个目录:

!mkdir file_destination
Run Code Online (Sandbox Code Playgroud)

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

!unzip file_location -d file_destination
Run Code Online (Sandbox Code Playgroud)


小智 5

对于 Python

连接到驱动器,

from google.colab import drive
drive.mount('/content/drive')
Run Code Online (Sandbox Code Playgroud)

检查目录

!ls!pwd

用于解压

!unzip drive/"My Drive"/images.zip
Run Code Online (Sandbox Code Playgroud)


Kor*_*ich 1

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

downloaded.GetContentFile('images.zip') 
Run Code Online (Sandbox Code Playgroud)

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