user14176475
user14176475

Reputation:

How to run %cd using a variable on ipython (google colab)?

I want to access folders shared to me on drive, I have created a shortcut on my drive and I wanna access it from there. The problem I'm facing is I want to change directory using variable. But when I use this code:

for boxNo in range(109):
  boxStr = "/gdrive/My Drive/Box Set {}".format(boxNo)
  %cd -q setStr  # The problem is here
  url = !pwd
  print("I have to use this url: " + url[0])

I get this error:

[Errno 0] No such file or directory: 'setStr'

SIDE-NOTE:
echo or !echo both don't work either as I write echo url[0] it displays url[0] on the output. But inside parenthesis, as in print, print(url[0]), it works. My guess is if I could change directory keeping the variable inside parenthesis it would work but I seem to find no way of doing so.

Upvotes: 2

Views: 1026

Answers (1)

Ahmad
Ahmad

Reputation: 9668

Use $ before the variable name

  %cd -q $setStr  

Upvotes: 1

Related Questions