How to use Python variables in Google Colab terminal command?

12,857

Solution 1

Try $. It will substitute the python variable.

!rm -r $name

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

Solution 2

Use {}.

myvar = "/test"
!cd {myvar}

According to https://colab.research.google.com/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/01.05-IPython-And-Shell-Commands.ipynb#scrollTo=q-zpo3vGM5Jv

Share:
12,857
me2 beats
Author by

me2 beats

Updated on June 26, 2022

Comments

  • me2 beats
    me2 beats almost 2 years

    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?