python子进程缺少参数

标签 python subprocess command-line-arguments

一段时间以来一直试图让这样的东西工作,下面似乎没有向输出 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/

相关文章:

python - 在一个对象中处理标签编码、转换和估计

python - 如何终止Python Popen创建的进程

python - 如何将父变量传递给python中的子进程?

python subprocess.call() 在将参数传递给 shell 脚本之前修改参数?

c# - 在 C# 中解析命令行参数的最佳方法?

c++ - 带有自定义 argc 和 argv 的 getopt_long() 函数

python - 是否可以手动调用 tensorboard smooth 函数?

python - 而不是 {% recursetree Nodes %} 中的 <li>

python - 简单的Python字符串代码\解码脚本问题

c++ - 将 ant 命令行选项传递给 exec'd ant 进程?