简体   繁体   English

Python:运行外部命令时传递参数

[英]Python: Passing arguments when running an external command

I am calling an external program like this 我打电话给这样的外部程序

call(["./myProgram", myArgs])

How can I pass a list of arguments? 我如何传递参数列表? myProgram takes 3 parameters like this myProgram有3个这样的参数

myProgram param1 param2 param3

specifiying arguments seperately like below works 单独指定论点如下所述

call(["./myProgram", param1 ,param2, param3])

, but how can I use a list/array of arguments, like ,但是如何使用参数的列表/数组,比如

myArgs=[param1,param2,param3]

I am getting this 我得到了这个

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception TypeError: execv() arg 2 must contain only strings

Just concatenate the lists: 只是连接列表:

call(['./myProgram'] + myArgs)

The first argument must be a list of strings; 第一个参数必须是字符串列表; simply build that list from two separate lists. 只需从两个单独的列表中构建该列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM