First post here but have been here many times while searching for answers.
I have finished my first coding book and am looking to add some functionality to a program I am writing. I am trying to pass a list where each numbers is one argument send to a PHP file.
This is the test code that I have been looking at.
import subprocess
list1 = [1, 2, 3, 4] #Eventual variable length list of arguments
args = ','.join(str(e) for e in list1) #Convert list to string
print(str1) #Print to screen
subprocess.call(["php","-f","/var/www/html/test.php", args]) #Send
This sends the data to a PHP file that looks like this.
#!/usr/bin/php
<?php
// Loop through each element in the $argv array
foreach($argv as $value)
{
echo "$value\n"; // Print each argument to terminal
}
?>
When it runs I get this result. 1,2,3,4
If I run it with the process call looking like this,
subprocess.call(["php","-f","/var/www/html/test.php", str(1),str(2),str(3),str(4)])
I get each of the numbers on a separate line (I think signifying that each is a new argument.)
How do I make it so that the subprocess.call will accept a list of arguments and actually have each comma-separated value be it's own argument?
EDIT Eventually, I will be using this to submit a variable list of sensor values to a server.
EDIT #2
Dang it. I did not realize till the post was marked as duplicate that the subprocess.call was working with a list. Makes a bunch more sense now.
Problem is that the subprocess.call is looking for a string lists where I have a string list and a integer list.
EDIT #3 Eventual correct solution if anyone cares.
import subprocess
list1 = [1.45736478, 2.234544, 3.87655, 4.99967]
args = [str(x) for x in list1]
print(args)
subprocess.call(["php","-f","/var/www/html/test.php"] + args)
Gives me a list of string values and outputs the values each on their own line.
Modified my other program based on what was here and it is running great. Thanks.