Lily Corne
Lily Corne

Reputation: 1

Can't access directory Tensorflow Google Colab

Sorry I'm new to Tensorflow2.1 andGoogleColab`. And I don't understand why I have this error :

My code :

%tensorflow_version 2.x

import tensorflow as tf
from tensorflow import keras

print(tf.__version__)

import pathlib
import os

path_data_dir = tf.keras.utils.get_file(origin='https://www.kaggle.com/c/dogs-vs-cats/download/0iMGwZllApFLiU35zX78%2Fversions%2Fm5lLqMS0KLfxJUozn3gR%2Ffiles%2Ftrain.zip',fname='train',untar= True)
data_dir = pathlib.Path(path_data_dir)

entries = os.listdir(data_dir)
for entry in entries:
   print(entry)

And I have this error (I tried to mount a GoogleDrive folder and I have access

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-1-88f88035f225> in <module>()
     12 data_dir = pathlib.Path(path_data_dir)
     13 
---> 14 entries = os.listdir(data_dir)
     15 for entry in entries:
     16    print(entry)

FileNotFoundError: [Errno 2] No such file or directory: '/root/.keras/datasets/train'

Thanks a lot for your help

Lily

Upvotes: 0

Views: 713

Answers (2)

Lily Corne
Lily Corne

Reputation: 1

I think this is simply a bad link to download data finally... On google colab I can't see correctly the downloaded file (because I can't see folders...) but I tried later on a computer and It's juste the link.

Upvotes: 0

Karim Elawaad
Karim Elawaad

Reputation: 160

I am assuming this is because of the different file system structure between a normal Linux machine and the runtime hosted by Google Colab.

As a workaround, pass the cache_dir='/content' argument to the get_file function to be as follows: path_data_dir = tf.keras.utils.get_file(origin='https://www.kaggle.com/c/dogs-vs-cats/download/0iMGwZllApFLiU35zX78%2Fversions%2Fm5lLqMS0KLfxJUozn3gR%2Ffiles%2Ftrain.zip',fname='train',untar= True, cache_dir='/content')

Be aware that the returned value path_data_dir is a full path to the file, so the function call os.list_dir(data_dir) will fail since data_dir points to a file and not a directory. To fix this, change entries = os.listdir(data_dir) to entries = os.listdir(data_dir.parent)

Upvotes: 1

Related Questions