0

I need to loop over a range of a variable and run 3 python scripts each of which uses the said variable. Currently I can run the 3 scripts without being able to loop thru the range of variable that the called scripts need. i currently have the following code:

import subprocess

cmd='python foo.py'
p=subprocess.Popen(cmd,shell=True)
out,err=p.communicate()

cmd='python bar.py'
p=subprocess.Popen(cmd,shell=True)
out,err=p.communicate()

cmd='python bas.py'
p=subprocess.Popen(cmd,shell=True)
out,err=p.communicate()

but what i need is something like the below:

for x in range(1, 11):
    cmd='python foo.py'
    p=subprocess.Popen(cmd,shell=True)
    out,err=p.communicate()

    cmd='python bar.py'
    p=subprocess.Popen(cmd,shell=True)
    out,err=p.communicate()

    cmd='python bas.py'
    p=subprocess.Popen(cmd,shell=True)
    out,err=p.communicate()

how can i pass x into foo, bar and bas and loop over the range of x? thank you for your help!

Sal
  • 3
  • 2
  • See https://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments – sashoalm Nov 16 '21 at 19:05
  • Are there reasons you can't just `import` them? – Axe319 Nov 16 '21 at 19:05
  • Hi Axe319, i'm not sure how to loop thru x with the import – Sal Nov 16 '21 at 19:10
  • hi sashoalm - i'm not sure the question you linked to answers my specific problem. could also be that my rather basic knowledge of python is preventing me from understanding it... – Sal Nov 16 '21 at 19:13
  • If you say passing x into foo, do you mean that foo should get the value of x? If so, why not pass it as a command-line parameter if it a simple variable? And if it is more complex, create a temporary pickle-file and pass the path to the file to foo. See: https://www.tutorialspoint.com/python/python_command_line_arguments.htm – C Hecht Nov 16 '21 at 19:19

0 Answers0