Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python运行命令和捕获结果-不将结果打印到屏幕_Python_Subprocess - Fatal编程技术网

Python运行命令和捕获结果-不将结果打印到屏幕

Python运行命令和捕获结果-不将结果打印到屏幕,python,subprocess,Python,Subprocess,我试图从python脚本运行命令,但是我想存储命令生成的输出,然后检查它是否有子字符串,但是它似乎没有存储在我的变量中,因为它仍然会打印到屏幕上 到目前为止我有这个 myfile = 'filename.txt' result = subprocess.Popen(['myprogram.exe', '-f' + myfile], stdout=subprocess.PIPE).communicate()[0] if result.find("error executing") != -1:

我试图从python脚本运行命令,但是我想存储命令生成的输出,然后检查它是否有子字符串,但是它似乎没有存储在我的变量中,因为它仍然会打印到屏幕上

到目前为止我有这个

myfile = 'filename.txt'
result = subprocess.Popen(['myprogram.exe', '-f' + myfile], stdout=subprocess.PIPE).communicate()[0]
if result.find("error executing") != -1:
     print "error!"
else:
     print "success!"

我对Python相当陌生。有人能解释一下为什么当我运行这个脚本时,myprogram.exe确实执行了,但它的输出仍然发送到屏幕上。如果我打印结果变量,它确实有来自myprogram.exe的额外输出,但我也需要显示错误的行。

您只是在重定向stdout。看起来您的程序将错误输出到stderr(如预期),将
stderr=subprocess.PIPE
添加到
Popen
调用。

您只是重定向stdout。看起来您的程序将错误输出到stderr(如预期),请将
stderr=subprocess.PIPE
添加到
Popen
调用。

尝试添加
stderr=subprocess.PIPE
。尝试添加
stderr=subprocess.PIPE