如何从本地驱动器将大数据上传并保存到Google Colaboratory?

2024-04-25 07:36:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经从这个Kaggle链接下载了大量的图像训练数据

https://www.kaggle.com/c/yelp-restaurant-photo-classification/data

如何有效地实现以下目标?

  1. 在Google colaborary中创建项目文件夹
  2. 将zip文件上载到项目文件夹
  3. 解压缩文件

谢谢

编辑:我尝试了下面的代码,但它对我的大zip文件崩溃。有没有更好/更有效的方法来实现这一点,我可以只指定文件在本地驱动器中的位置?

from google.colab import files
uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

Tags: 文件数据项目namehttps图像文件夹链接
3条回答
!pip install kaggle
api_token = {"username":"USERNAME","key":"API_KEY"}
import json
import zipfile
import os
with open('/content/.kaggle/kaggle.json', 'w') as file:
    json.dump(api_token, file)
!chmod 600 /content/.kaggle/kaggle.json
!kaggle config set -n path -v /content
!kaggle competitions download -c jigsaw-toxic-comment-classification-challenge
os.chdir('/content/competitions/jigsaw-toxic-comment-classification-challenge')
for file in os.listdir():
    zip_ref = zipfile.ZipFile(file, 'r')
    zip_ref.extractall()
    zip_ref.close()

第9行有微小的变化,没有发生错误。 来源:https://gist.github.com/jayspeidell/d10b84b8d3da52df723beacc5b15cb27 无法添加为评论原因代表

您可能需要使用kaggle-cli模块来帮助下载。

this fast.ai thread中讨论过。

您可以使用这些线程:

也可以查看I/O example notebook。例如,要访问xls文件,您需要将文件上载到Google工作表。然后,可以在同一个I/O示例笔记本中使用gspread配方。

相关问题 更多 >