-1

I want to use python3 command line to run some scirpt that get some output. I can't import python scirpt because my code have to work with many of scripting writing in python independent of me and I can't do this manually. I want to run like this result:

sensor_name = "./sensors/" + sensor + ".py"
result = subprocess.run(['python3',sensor_name,'22','2','temperature'], stdout=subprocess.PIPE)

When arguments {22,2,temperature} is writing now manually. I want to replace this arguments with 1 variable like this :

    result = subprocess.run(['python3',sensor_name, temp], stdout=subprocess.PIPE)

I trying to insert here list but it doesn't support this type of variable. I try to use string spliting commas but when I put string I get only 1 arguments. Script which I want to run looks like this :

if __name__=="__main__":
    print(dht(*sys.argv[1:]))

And function dht :

def dht(dht_type,pin,quanity,quanity1):

Any suggestions how to deal with it ?

Rysiusek
  • 17
  • 4

1 Answers1

0

You can use the * ("splat") operator to "expand" a list into an other one e.g.

>>> sensor_name = 'a'
>>> temp = ['22', '2', 'temperature']
>>> ['python3', sensor_name, *temp]
['python3', sensor_name, '22', '2', 'temperature']

Neither dht nor its formal parameters are anywhere in the subprocess call so I've no idea why you added them.

Masklinn
  • 34,759
  • 3
  • 38
  • 57