Search code examples
pythongoogle-colaboratory

How to access files on Google Colab notebook


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())

I get an error: FileNotFoundError


Solution

  • 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')
    

    copy over the folder to /content/ using !cp /content/drive/My\ Drive/PetImages.zip /content/ then use !unzip PetImages.zip

    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.