I have a script which calls another python script by subprocess.Popen module. But since I have arguments stored in variable(s)
servers[server]['address']
servers[server]['port']
servers[server]['pass']
I am unable to perform the command
p = subprocess.Popen(["python mytool.py -a ", servers[server]['address'], "-x", servers[server]['port'], "-p", servers[server]['pass'], "some additional command"], shell=True, stdout=subprocess.PIPE)
Workaround:
I got it to work by combing @wilberforce & @ciphor's answer but by a little modification
command = "python mytool.py -a %s -x %s -p %s some additional command" % (servers[server]['address'], servers[server]['port'], servers[server]['pass'])
p = subprocess.Popen(command , shell=True, stdout=subprocess.PIPE)
It ceases to work if I added several variables in double quotes, it could take at max "2" variables and break I added more.
Thanks to everyone who answered!
You should concatenate the command to a whole string:
When you call
subprocess.Popen
you can pass either a string or a list for the command to be run. If you pass a list, the items should be split in a particular way.In your case, you need to split it something like this:
This is because if you pass in a list,
Popen
assumes you have already split the command line into words (the values that would end up insys.argv
), so it doesn't need to.The way you're calling it, it will try to run a binary called "python mytool.py -a", which isn't what you meant.
The other way to fix it is to join all of the words into a string (which
Popen
will then split up - seesubprocess.list2cmdline
). But you're better off using the list version if possible - it gives simpler control of how the commandline is split up (if arguments have spaces or quotes in them, for example) without having to mess around with quoting quote characters.Your problem in type
str
for firstPopen
argument. Replace it tolist
. Below code can work:Furthermore you can play with
shell
, because it system-depend parameter.Drop
shell=True
. The arguments toPopen()
are treated differently on Unix ifshell=True
:Note that passing
shell=True
for commands with external input is a security hazard, as described by a warning in the docs.