0

I have a scenario, where I have to call a certain Python script multiple times in another python script.

script1:

import sys

path=sys.argv

print "I am a test"
print "see! I do nothing productive."
print "path:",path[1]

script2:

import subprocess

l=list()
l.append('root')
l.append('root1')
l.append('root2')

for i in l:
    cmd="python script1.py i"
    subprocess.Popen(cmd,shell=True)

Here, my issue is that in script 2, I am not able to replace the value of "i" in the for loop. Can you help with that?

1
  • Just create the string in the for loop. cmd = "python script1.py " + str(i). You are hardcoding i as a character, and not using its value. Commented Apr 10, 2016 at 15:09

3 Answers 3

1

to substitute the value of i into the string you can concatenate it:

cmd="python script1.py "+i

or format it into the string:

cmd="python script1.py %s"%i

Either way you need to use the variable i instead of the string i.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are looking for this:

cmd="python script1.py %s" % i

Comments

0

Use subprocess.Popen with a lists:

import subprocess

paths = ['root', 'root1', 'root2']

for path in paths:
    subprocess.Popen(['python', 'script1.py', path])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.