[英]setting variable to output of c program with command line arguments in python
I am trying to use a python script in which a variable is set to the output of ac program test.c which requires command line arguments. 我正在尝试使用python脚本,其中将变量设置为需要命令行参数的ac程序test.c的输出。 Below is my c program: 下面是我的C程序:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(int argc, char *argv[])
{
int lat;
lat=atoi(argv[1]);
printf("-->%d\n",lat);
}
The python program is: python程序是:
import subprocess
for lat in range(80,-79,-1):
cmd = '"./a.out" {}'.format(lat)
print "cmd is ",cmd
parm31=subprocess.call(cmd,shell=True)
print "parm is ",parm31
I have compiled test.c to get a.out. 我已经编译了test.c以获得a.out。 My goal is to have an output when running the python program with the c program (test.c or a.out) embedded in it is to have an output of: 我的目标是在运行带有c程序(test.c或a.out)的python程序时,输出为:
parm is -->80
parm is -->79
parm is -->78
...
parm is -->-77
parm is -->-78
I am unfortunately not getting to output but other values for the number component of the variable and some other undesired output. 不幸的是,我没有输出,而是变量的数字部分的其他值以及其他一些不希望的输出。 How do I tweak this program in order to get the correct output? 我如何调整该程序以获得正确的输出?
According to [Python 2.Docs]: subprocess. 根据[Python 2.Docs]:子进程。 call ( args, *, stdin=None, stdout=None, stderr=None, shell=False ) ( emphasis is mine): 调用 ( args,*,stdin = None,stdout = None,stderr = None,shell = False ) ( 重点是我的):
Run the command described by args . 运行args描述的命令。 Wait for command to complete, then return the
returncode
attribute . 等待命令完成, 然后返回returncode
属性 。
To make things work: 使事情起作用:
shell=True
不要通过shell=True
import subprocess
for lat in range(80, -79, -1):
cmd = ["\"./a.out\"", "{}".format(lat)]
print "Command is ", " ".join(cmd)
out = subprocess.check_output(cmd)
print "Command output is ", out.strip()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.