Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Python variables in Google Colab terminal command?

I have a cell with this content:

import os os.mkdir('123') name = '123' !rm -r name 

I want to pass the value of the variable name to the last line. How can I do this?

like image 449
me2 beats Avatar asked Oct 17 '18 09:10

me2 beats


People also ask

How do you use Terminal command in Colab?

It's already possible to run terminal commands in a Colab notebook — all you need to do is prepend an exclamation to the command, or use the %%shell command, but sometimes you might prefer the convenience an interactive shell.

How do you define a variable in Colab?

To create a variable, provide an initial value. The tf. Variable will have the same dtype as the initialization value. A variable looks and acts like a tensor, and, in fact, is a data structure backed by a tf.

How do I run all commands in Google Colab?

To simply have all the cells run automatically, click Runtime > Run all in the colab toolbar.


1 Answers

Try $. It will substitute the python variable.

!rm -r $name 

You may need to use $name\.txt or {name}.txt in some cases.

like image 164
korakot Avatar answered Sep 20 '22 09:09

korakot