无法从Python正确解析bash的输出
在下面的Python代码中,我通过在Python脚本中嵌入bash命令来调用Makefile。我想检查是否在标准输出上报告了警告。我确实看到了标准输出上的警告,但我的Python代码似乎没有检测到它 Python代码:无法从Python正确解析bash的输出,python,bash,parsing,stdout,Python,Bash,Parsing,Stdout,在下面的Python代码中,我通过在Python脚本中嵌入bash命令来调用Makefile。我想检查是否在标准输出上报告了警告。我确实看到了标准输出上的警告,但我的Python代码似乎没有检测到它 Python代码: maker = subprocess.Popen(["bash", "-c", "make"], stdout=subprocess.PIPE) for line in maker.stdout: if "warning:" in line:
maker = subprocess.Popen(["bash", "-c", "make"], stdout=subprocess.PIPE)
for line in maker.stdout:
if "warning:" in line:
print "Warning(s) detected in make"
stdout上的输出清楚地报告警告:
main.c: In function ‘main’:
main.c:46:14: warning: unused variable ‘options’ [-Wunused-variable]
还可以尝试捕获stderr:
subprocess.Popen(["bash", "-c", "make"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(正如用户“Barmar”已经指出的,编译器错误消息被发送到stderr。)也尝试捕获stderr:
subprocess.Popen(["bash", "-c", "make"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(正如用户“Barmar”已经指出的,编译器错误消息被发送到stderr。)编译器错误消息被打印到标准错误,而不是标准输出。编译器错误消息被打印到标准错误,而不是标准输出。