首页 > 解决方案 > 如何访问从 kaggle 下载到 Colaboratory notebook 的文件?

问题描述

我在操作 Colaboratory Notebook 中的多个文件时遇到了一些困难,该笔记本下载到我的谷歌驱动器中的 /content 目录。到目前为止,我已经使用以下代码成功下载并提取了一个 kaggle 数据集到 Colaboratory Notebook:

!kaggle datasets download -d iarunava/cell-images-for-detecting-malaria -p /content
!unzip \cell-images-for-detecting-malaria.zip

我还能够使用 Pillow 将数据集中的单个文件导入到我的 Colaboratory 会话中(我从提取过程中产生的输出中获得了文件名):

from PIL import Image
img = Image.open('cell_images/Uninfected/C96P57ThinF_IMG_20150824_105445_cell_139.png')

如何在不事先知道名称的情况下从 /content 访问多个提取的文件?

谢谢!

标签: python-3.ximage-processinggoogle-colaboratorykaggle

解决方案


经过一些进一步的实验,我发现 python os 模块在 Colab Notebooks 中的工作方式与在个人计算机上的工作方式相似。例如,在 Colab Notebook 中,命令

os.getcwd()

返回 '​​/content' 作为输出。

此外,该命令os.listdir()返回我下载和提取的所有文件的名称。


推荐阅读