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

Python 从Google colab笔记本中提取Google Drive zip,python,google-drive-api,google-colaboratory,zipfile,Python,Google Drive Api,Google Colaboratory,Zipfile,我已经在谷歌硬盘上有一个压缩的(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 # Authenticat

我已经在谷歌硬盘上有一个压缩的(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错误”


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

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

downloaded.GetContentFile('images.zip') 

然后,您可以稍后使用
unzip

解压,您只需使用

!unzip file_location

简单的连接方式

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}

要从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()
格德里火山:

from google.colab import drive
drive.mount('/content/gdrive')
打开链接->复制授权码->将其粘贴到提示中,然后按“回车”

检查GDrive访问:

!ls "/content/gdrive/My Drive"
(q代表“安静”)来自GDrive的文件:

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

首先,在colab上安装解压:

!apt install unzip
然后使用解压缩来解压缩文件:

!unzip  source.zip -d destination.zip

首先创建一个新目录:

!mkdir file_destination
!unzip path_to_file.zip -d path_to_directory
现在,是时候用解压文件膨胀目录了,如下所示:

!unzip file_location -d file_destination

要将文件解压缩到目录,请执行以下操作:

!mkdir file_destination
!unzip path_to_file.zip -d path_to_directory
对于Python

连接到驱动器

from google.colab import drive
drive.mount('/content/drive')
检查目录

!ls
!pwd

解压

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

安装到驱动器上后,请使用。它适用于几乎所有的归档格式(例如,“zip”、“tar”、“gztar”、“bztar”、“xztar”),而且非常简单:

import shutil
shutil.unpack_archive("filename", "path_to_extract")
试试这个:

!unpack file.zip
如果它现在工作或文件为7z,请尝试下面的内容

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

Colab研究团队有一个帮助你的团队

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

!解压-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')

我希望这有帮助!干杯

在我看来,你必须走某条路,例如:

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

然后:

!!apt安装解压 !解压\u文件夹.zip-d解压\u文件夹

这似乎有效,但可能太快了。但后来解压不起作用。存档:未找到中心目录签名的flowers.zip结尾。此文件不是zipfile,或者它构成多部分存档的一个磁盘。在后一种情况下,将在此存档的最后一个磁盘上找到中心目录和zipfile注释。解压:在flowers或flowers.zip中找不到zipfile目录,也找不到flowers.zip,句号。对于代码部分,请使用特定于语言的格式(在此上下文中为Python)。添加了,感谢解压,首先将zip文件复制到本地Colab存储,然后执行解压操作,这不是更好吗?
from google.colab import drive 
drive.mount('/content/drive')