Send arbitrary number of inputs from python to .exe
p = subprocess.Popen(args = "myprog.exe" + " " +
str(input1) + " " +
str(input2) + " " +
str(input3) + " " +
strpoints, stdout = subprocess.PIPE)
in the above code, input1, input2 and input3 are integers that are converted to strings. the "strpoints" variable is a list of arbitrary length strings. input1 tells myprog the length of strpoints. of course, when I try to run the above code, I get the following error:
TypeError: Cannot convert object 'list' to str implicitly
how to pass all strpoints elements to myprog.exe? am I doomed to have to execute str (strpoints) and then myprog.exe parse that for commas, apostrophes, etc.? eg.
`โ> x = ['a', 'b']
`โ> str (x)
"['a', 'b']"
or should I create a huge string beforehand? eg.
'โ> x = [' a ',' b ']
'โ> stringify (x)
'ab'
where stringify would be something like
def stringify(strlist):
rlist = ""
for i in strlist:
rlist = rlist + i + " "
return rlist
source to share
Any thought you might want to enclose these strings in quotes if they have embedded spaces? Something like:
if strpoints:
finalargs = '"' + '" "'.join(strpoints) + '"'
else:
finalargs = ""
p = subprocess.Popen(args = "myprog.exe" + " " +
str(input1) + " " +
str(input2) + " " +
str(input3) + " " +
finalargs, stdout = subprocess.PIPE)
This makes your string longer, but preserves the integrity of your individual items in the list.
source to share