26

I can't change my directory in Google colab. when I type cd it gives output like this '/content' I tried to change the directory using import os os.chdir("drive")

but it shows error No such file or directory: 'drive' . How to get rid of this?

korakot
  • 37,818
  • 16
  • 123
  • 144
Mushahid Shamim
  • 274
  • 1
  • 3
  • 10

6 Answers6

41

If you want to change directory from google colab into google drive, connect to google drive first.

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

It will ask for authentication key that will be can be obtained when signing in to your google drive

Change to the google drive directory.

%cd /content/drive/MyDrive/

For verifying: !pwd

Your current directory is /content/drive/MyDrive/

Edward Edberg
  • 591
  • 5
  • 6
  • Transport endpoint is not connected: '/content/drive/My Drive/' . i am getting this error – Sandrin Joy Dec 10 '20 at 08:46
  • Yes, because this answer is slightly outdated. Now the same path should be: '/content/drive/MyDrive/'. – Googr Aug 05 '21 at 12:36
  • @Googr it doesn't seem to work for me. If I type !ls, I can see the drive folder. But if I type !cd /content/drive/MyDrive/ (or !cd drive/MyDrive/) and then !pwd, I still get /content. Is there anything else I need to do for the !cd command to work? – JohnDoe122 Feb 09 '22 at 15:25
  • @BillKet, try %cd instead of !cd. – Googr Feb 10 '22 at 09:01
  • Just `cd` seems to work as of now. – passerby51 Mar 21 '22 at 06:23
28

Did you create a /drive directory? (By default, one does not exist. You'll need to mount your Drive using a recipe like this one.

Otherwise, create a directory first. For example,

!mkdir demo
%cd demo
!pwd

will show /content/demo.

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
9

First check for the current directory you are working in,

!pwd

Now, if you want to change the present working directory then,

import os
os.chdir('[path you want to move in]')

suppose path is

os.chdir('/content/drive/My Drive/Colab Notebooks/Data')

Now the directory 'data' is the current working directory. You can check,

!pwd

/content/drive/My Drive/Colab Notebooks/Data This indicates your directory has been changed successfully. You can check the full example here. https://colab.research.google.com/drive/1CSPBdmY0TxU038aKscL8YJ3ELgCiGGju#scrollTo=SWeWe_Bb8wO0

Nikita Bachani
  • 541
  • 6
  • 9
3

First, you have to install OCaml Fuse to use this feature on Google Colab, run the following command to install OCaml Fuse :

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

During the installation, (usually) Collab will ask you to insert code verification twice, just follow the link and accept the permission, then copy the code and paste to the form field that appear.

After the installation finish, run the below :

!mkdir drive
!google-drive-ocamlfuse drive
!ls -lla drive/"Colab Notebooks"

and see the output, and then you can refer to your file on your drive like these :

x = "drive/Colab Notebooks/blablablah/blah/file.p"
y = "drive/Colab Notebooks/blablablah/blah/file.p"
z = "drive/Colab Notebooks/blablablah/blah/file.p"

hope this can help you!

Imam Digmi
  • 452
  • 4
  • 14
1

/content/ is the default directory in which all of your files and data are saved. To change the directory you can do something like this: Create a folder inside the /content/ which is recommended. Then run this: import os os.chdir('/content/folder_name') If you run !pwd you could see the path changed to /content/folder_name

Mayank Pathela
  • 453
  • 1
  • 6
  • 15
-3
%cd /content/drive/MyDrive/folder/

!pwd

This will change your directory to the path you have set. You can check your changed dir by typing !pwd.

darkexodus
  • 147
  • 1
  • 14