如何访问Google Colab笔记本上的文件 [英] How to access files on Google Colab notebook
问题描述
如何访问Google Colab左侧栏中的文件?默认情况下,给出了示例数据,当我运行!ls sample_data
命令时,它会列出应有的文件:
How can I access the files on the left bar of Google Colab? By default sample data is given, when I run !ls sample_data
command it lists the files as it should:
anscombe.json mnist_test.csv
california_housing_test.csv mnist_train_small.csv
california_housing_train.csv README.md
但是当我运行以下代码时:
But when I run following code:
import pandas as pd
data = pd.read_csv('content/sample_data/mnist_test.csv')
print(data.head())
我收到一个错误:FileNotFoundError
I get an error: FileNotFoundError
推荐答案
如果不移动文件,使用上传会将文件放在/下,因此请尝试/PetImages/Dogs/,也可以将其上传到Google驱动器并挂载google驱动器可让您访问在单元格中运行此文件的colab文件中的文件
using upload will place the files under / if you dont move them, so try /PetImages/Dogs/, alternatively you can upload them to google drive and mount your google drive giving access to your files in colab running this in a cell
from google.colab import drive
drive.mount('/content/drive')
使用以下方式将文件夹复制到/content/
!cp /content/drive/My\ Drive/PetImages.zip /content/
然后使用!unzip PetImages.zip
copy over the folder to /content/ using
!cp /content/drive/My\ Drive/PetImages.zip /content/
then use !unzip PetImages.zip
现在您的目录将为/content/PetImages/Dogs/,由于将目录上载到colab的速度较慢,并且在12小时后被删除,因此可能会更容易.
now your directory will be /content/PetImages/Dogs/ which might be easier as uploading directory to colab is slower and gets deleted after 12 hours.
这篇关于如何访问Google Colab笔记本上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!