python 子进程缺少参数

jay*_*jay 2 python subprocess command-line-arguments

一直试图让这样的东西工作一段时间,下面似乎没有将正确的arg发送到c程序arg_count,它输出argc = 1. 当我非常确定我希望它是这样的时候2./arg_count -arg从 shell 输出 2...

我尝试过使用另一个参数(因此它会在 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
Run Code Online (Sandbox Code Playgroud)

知道我在哪里摔倒了吗?顺便说一句,我正在运行Linux。

小智 5

文档中:

shell 参数(默认为 False)指定是否使用 shell 作为要执行的程序。如果 shell 为 True,建议将 args 作为字符串而不是序列传递。

因此,

pipe = subprocess.Popen("./args/Release/arg_count -arg", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)

应该给你你想要的。