我正在尝试让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中,我正在使用:
temp = subprocess.Popen("""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);}'
""", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
...然后从temp读取/打印行。问题是Python不会将最后三个打印语句(都是整数)打印到标准输出,或者至少我无法找到它。 gvpr程序的其余部分可以在Python中正常工作。
谢谢!
答案 0 :(得分:0)
您可以将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,例如:
f = open(std_out_file, "r")
stdout = f.readlines()
f.close()
您可以检查命令的返回码,检查是否还需要打印这样的stderr -
if p.returncode == 0:
答案 1 :(得分:0)
经过一些工作后,我将BEGIN引号更改为double,并将所有内部参数更改为single,这似乎解决了问题。