I just need a hint on how to do things properly.
Say I have a script called script.py which uses a list of names as argument ["name1", "name2", etc. ].
I want to call this script from another script using the subprocess module. So what I would like to do is the following :
myList = ["name1", "name2", "name3"]
subprocess.Popen(["python", "script.py", myList])
Of course that doesn't work because the subprocess.Popen method requires a list of strings as arguments.
So I considered doing the following :
subprocess.Popen(["python", "script.py", str(myList)])
Now the process starts but it doesn't work because it has a string as argument and not a list. How should I fix that properly?
Concatenate them using +
operator.
myList = ["name1", "name2", "name3"]
subprocess.Popen(["python", "script.py"] + myList)
BTW, if you want use same python program, replace "python"
with sys.executable
.
Thanks for the quick answer falsetru. It doesn't work directly but I understand how to do.
You're suggestion is equivalent to doing :
subprocess.Popen(["Python","script.py","name1","name2","name3"])
Where I have 3 arguments that are the strings contained in my original list.
All I need to do in my script.py file is to build a new list from each argument received by doing the following :
myList = sys.argv[1:]
myList is now the same than the one I had initially!
["name1","name2","name3"]
Don't know why I didn't think about it earlier!