Home > Enterprise >  how to run bash code in a loop in google colab
how to run bash code in a loop in google colab

Time:11-20

I am trying to run a loop that requires the bash command -- !python3 -m runner.player_1

but when I make it into loop:

for player1 in range(0, 100, 1):
    !python3 -m "runner.player_"   str(player1)

it doesn't work and returns the error:

/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `python3 -m "runner.player_"   str(player1)'

how can i fix this? thank you

CodePudding user response:

You will have to call it as python function using os.system, not as magic command.

import os
for player1 in range(0, 100, 1):
    os.system("python3 -m runner.player_"   str(player1))

CodePudding user response:

A native Bash loop would look like

for i in {0..99}; do python3 -m runner.player_$i; done

You can replace the semicolons with newlines, and/or add a newline after do if you like. I'm guessing you will want it literally as a one-liner.

This seems like an XY problem, though; surely it would be better if whatever code implements these 100 modules would be refactored so that you can run them all sequentially in one go.

  • Related