一段时间以来一直试图让这样的东西工作,下面似乎没有向输出 argc = 1
的 c 程序 arg_count 发送正确的 arg。当我非常确定我希望它是 2
时。 ./arg_count -arg
从 shell 输出 2...
我已经尝试使用另一个 arg(因此它会在 shell 中输出 3)并且在通过子进程调用时它仍然输出 1。
import subprocess
pipe = subprocess.Popen(["./args/Release/arg_count", "-arg"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = pipe.communicate()
result = out.decode()
print "Result : ",result
print "Error : ",err
知道我在哪里摔倒了吗?顺便说一句,我正在运行 Linux。
最佳答案
来自documentation :
The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.
因此,
pipe = subprocess.Popen("./args/Release/arg_count -arg", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
应该给你你想要的。
关于python子进程缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19377827/