让Python打印到命令行(graphviz命令gvpr的输出)

2024-09-25 00:35:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让Python运行这个命令,它在我的命令提示符下运行良好:

ccomps -x rel_graph.dot | gvpr -c "N[nNodes($G)<5]{delete(0,$)}" | dot | gvpack | sfdp -Goverlap=prism | gvmap -e | gvpr "BEGIN{int m,w,e = 0} N[fontcolor=='blue']{m += 1} N[fontcolor=='green']{e += 1} N[fontcolor=='red']{w += 1} END{print(m); print(w); print(e);}"

在Python中,我使用:

^{pr2}$

…然后从temp读取/打印行。问题是Python没有将最后三个print语句(都是整数)打印到标准输出,或者至少我找不到它。gvpr程序的其余部分在Python中运行良好。在

谢谢!在


Tags: 命令deletedotgraphrelprint命令提示符prism
2条回答

在做了更多的工作之后,我将开始引号改为double,所有内部参数都改为single,这似乎解决了这个问题。在

您可以将stdout\stderr发送到这样的文件-

from subprocess import Popen
std_out_file = "/tmp/stdout.log"
std_err_file = "/tmp/stderr.log"
command_to_execute = "<your-command>"
with open(std_out_file, "wb") as out, open(std_err_file, "wb") as err:
        p = Popen(command_to_execute, shell=True, cwd=<folder>, stdout=out, stderr=err)
p.communicate()

然后从文件中读取stdout\stderr,例如:

^{pr2}$

您可以检查命令的返回代码,以检查是否还需要像这样打印stderr-

if p.returncode == 0:

相关问题 更多 >