Changing directory in Google colab (breaking out of the python interpreter)

Google Colaboratory

Google Colaboratory Problem Overview


So I'm trying to git clone and cd into that directory using Google collab - but I cant cd into it. What am I doing wrong?

>!rm -rf SwitchFrequencyAnalysis && git clone https://github.com/ACECentre/SwitchFrequencyAnalysis.git > >!cd SwitchFrequencyAnalysis > >!ls > datalab/ SwitchFrequencyAnalysis/

You would expect it to output the directory contents of SwitchFrequencyAnalysis - but instead its the root. I'm feeling I'm missing something obvious - Is it something to do with being within the python interpreter? (where is the documentation??)

Demo here.

Google Colaboratory Solutions


Solution 1 - Google Colaboratory

use

to change the current working directory for the notebook environment (and not just the subshell that runs your ! command).

you can confirm it worked with the pwd command like this:

further information about jupyter / ipython magics: http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-cd

Solution 2 - Google Colaboratory

As others have pointed out, the cd command needs to start with a percentage sign:

%cd SwitchFrequencyAnalysis

Difference between % and !

Google Colab seems to inherit these syntaxes from Jupyter (which inherits them from IPython). Jake VanderPlas explains this IPython behaviour here. You can see the excerpt below.

> If you play with IPython's shell commands for a while, you might > notice that you cannot use !cd to navigate the filesystem: > > > In [11]: !pwd > /home/jake/projects/myproject > > In [12]: !cd .. > > In [13]: !pwd > /home/jake/projects/myproject > > > The reason is that > shell commands in the notebook are executed in a temporary subshell. > If you'd like to change the working directory in a more enduring way, > you can use the %cd magic command: > > In [14]: %cd .. > /home/jake/projects >

Another way to look at this: you need % because changing directory is relevant to the environment of the current notebook but not to the entire server runtime.

In general, use ! if the command is one that's okay to run in a separate shell. Use % if the command needs to be run on the specific notebook.

Solution 3 - Google Colaboratory

Use os.chdir. Here's a full example: https://colab.research.google.com/notebook#fileId=1CSPBdmY0TxU038aKscL8YJ3ELgCiGGju

Compactly:

!mkdir abc
!echo "file" > abc/123.txt

import os
os.chdir('abc')

# Now the directory 'abc' is the current working directory.
# and will show 123.txt.
!ls

Solution 4 - Google Colaboratory

If you want to use the cd or ls functions , you need proper identifiers before the function names ( % and ! respectively) use %cd and !ls to navigate

.

!ls    # to find the directory you're in ,
%cd ./samplefolder  #if you wanna go into a folder (say samplefolder)

or if you wanna go out of the current folder

%cd ../      

and then navigate to the required folder/file accordingly

Solution 5 - Google Colaboratory

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

view this answer for detailed explaination https://stackoverflow.com/a/61636734/11535267

Solution 6 - Google Colaboratory

I believe you'd have to mount the Google Drive first before you do anything else.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwillwadeView Question on Stackoverflow
Solution 1 - Google ColaboratoryFabian LinzbergerView Answer on Stackoverflow
Solution 2 - Google ColaboratorySimon SeoView Answer on Stackoverflow
Solution 3 - Google ColaboratoryBob SmithView Answer on Stackoverflow
Solution 4 - Google ColaboratoryzsfVishnuView Answer on Stackoverflow
Solution 5 - Google ColaboratoryNikita BachaniView Answer on Stackoverflow
Solution 6 - Google ColaboratoryThirty OneView Answer on Stackoverflow