Python 如何使用此函数的argumensts中的变量运行subprocess.run?
我想使用python3命令行运行一些scirpt以获得一些输出。我无法导入python scirpt,因为我的代码必须独立于我使用python编写脚本 我不能手动完成这项工作。我想这样运行结果:Python 如何使用此函数的argumensts中的变量运行subprocess.run?,python,python-3.x,Python,Python 3.x,我想使用python3命令行运行一些scirpt以获得一些输出。我无法导入python scirpt,因为我的代码必须独立于我使用python编写脚本 我不能手动完成这项工作。我想这样运行结果: sensor_name = "./sensors/" + sensor + ".py" result = subprocess.run(['python3',sensor_name,'22','2','temperature'], stdout=subproces
sensor_name = "./sensors/" + sensor + ".py"
result = subprocess.run(['python3',sensor_name,'22','2','temperature'], stdout=subprocess.PIPE)
当参数{22,2,temperature}正在手动写入时。我想用1个变量替换此参数,如下所示:
result = subprocess.run(['python3',sensor_name, temp], stdout=subprocess.PIPE)
if __name__=="__main__":
print(dht(*sys.argv[1:]))
我试图在此处插入列表,但它不支持这种类型的变量。
我尝试使用字符串拆分逗号,但当我放入字符串时,我只得到1个参数。
我要运行的脚本如下所示:
result = subprocess.run(['python3',sensor_name, temp], stdout=subprocess.PIPE)
if __name__=="__main__":
print(dht(*sys.argv[1:]))
和功能dht:
def dht(dht_type,pin,quanity,quanity1):
有什么建议吗?您可以使用
*
(“splat”)操作符将列表“展开”为另一个列表,例如
>>> sensor_name = 'a'
>>> temp = ['22', '2', 'temperature']
>>> ['python3', sensor_name, *temp]
['python3', sensor_name, '22', '2', 'temperature']
dht
及其形式参数都不在子流程中的任何位置,因此我不知道您为什么要添加它们。非常感谢,这很有效。传感器='dht'