Python调用系统程序无法正常工作

时间:2014-11-10 02:14:06

标签: python linux gcc

我写了一个小的python脚本,打算为gcc自动化非默认选项(在Kubuntu 14.04上); python现在运行没有错误,并插入调试print语句(或将系统命令更改为' echo')验证正在传递的信息是正确的,但是我从gcc那里得到错误

$ python gccm prog16
gcc: fatal error: no input files 
compilation terminated.

这是我写的剧本:

#!/usr/bin/python
from sys import argv #get incoming argument
import subprocess #function to call an OS program

script, target = argv

# massage received argument into form needed for math.h linkage
target = "-o " + target + " " + target + ".c -lm"
subprocess.call (['gcc', target], shell=False)`

如果我可以正常工作,我还会对gcc调用(编译版本选项,更严格的代码检查等)做出其他补充。根据错误消息,它似乎正在正确调用gcc,但目标源文件未被找到;这可能不会在我调用它的目录中运行吗?如果是这样,我怎样才能从正确的目录(我保留我的C源代码文件)中运行它;如果没有,还有什么可能导致这种情况?

2 个答案:

答案 0 :(得分:1)

如果您使用shell=False,则不应将您对子流程的参数连接在一起。相反,它们应该是args列表中的每个元素:

subprocess.call(['gcc', '-o', target, target+'.c', '-lm'], shell=False)

在相关的说明中,你自己写这样的东西的任何理由?如果您希望使用基于Python的构建系统,请查看SCons

答案 1 :(得分:1)

如果您有shell=False,则必须将每个参数分别传递到subprocess.call

请改为尝试:

subprocess.call (['gcc', '-o', target, target + '.c', '-lm'], shell=False)