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?
To pass variables to Python subprocess. Popen, we cann Popen with a list that has the variables we want to include. to call Popen with the command list that has some static and dynamic command arguments.
You can pass a string to the command to be executed by subprocess. run method by using “input='string'” argument. As you can see, the code above passes “data. txt” as a string and not as a file object.
From the docs: args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).
How do I pass a string into subprocess.Popen (using the stdin argument)? Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.
The simples use case to create a subprocess is using call () function. call () function accepts related binary or executable name and parameters as Python list. In this example, we will call Linux ls command with -l and -a parameters.
The Popen () method can be used to create a process easily. As stated previously the Popen () method can be used to create a process from a command, script, or binary. In the following example, we create a process using the ls command. import subprocess subprocess.Popen ("ls -al",shell=True)
like: os.system, os.spawn*, os.popen*, popen2.* commands. Let’s start looking into the different functions of subprocess. Run the command described by “args”. Note, the default value of the shell argument is False.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With