3

I have a directory named ML in my drive: which has some of my colab-notebooks and some csv files which I want to load in my colab-notebook.

But , when I use !pwd to find out the current folder, its output is as follows.

!pwd
/content

When I use !ls .. The directory structure is as follows:

bin/
boot/
colabtools/
content/
datalab/
dev/
etc/
gpu-tensorflow-1.9.0-cp27-cp27mu-linux_x86_64.whl
gpu-tensorflow-1.9.0-cp36-cp36m-linux_x86_64.whl
home/
lib/
lib64/
media/
mnt/
opt/
proc/
root/
run/
sbin/
srv/
sys/
tensorflow-1.9.0-cp27-cp27mu-linux_x86_64.whl
tensorflow-1.9.0-cp36-cp36m-linux_x86_64.whl
tf_deps/
tmp/
tools/
usr/
var/

I am unable to !cd to my directory which is drive/ML , since I don't know the path, and the tree structure and thus, load my csv files.

aspiring1
  • 344
  • 1
  • 13
  • 32

2 Answers2

3

use %cd instead of !cd, like this:

%cd drive/ML
korakot
  • 37,818
  • 16
  • 123
  • 144
  • I get this error, I'm in the /content directory [Errno 2] No such file or directory: 'drive/ML' /content – aspiring1 Jul 19 '18 at 09:34
  • The directory is not your google drive. You need to map it first. See https://stackoverflow.com/questions/50606118/how-to-read-files-inside-a-folder-in-drive-inside-a-notebook – korakot Jul 19 '18 at 16:51
  • Thanks, I got it figured out now, my-google drive wasn't mounted in the first place and so, I didn't get a directory for my drive in the /contents directory. Thanks a lot. – aspiring1 Jul 19 '18 at 17:22
1

In such cases we need to mount our google-drive and it will be shown as below:

os.listdir()
/content

And,

os.listdir('/content')

datalab  drive

If the google-drive isn't mounted the /content directory will only contain the datalab folder.

To mount your Google Drive:

from google.colab import drive
drive.mount('/content/drive')

For further information refer here

Afterwards, to change directory use:

import os
os.chdir("drive/My Drive/ML")
aspiring1
  • 344
  • 1
  • 13
  • 32