Python,从shell命令捕获整个输出

Python,从shell命令捕获整个输出,shell,python-2.7,output,Shell,Python 2.7,Output,我正在使用Python自动化我在机器学习中所做的一些工作。我使用的机器学习工具是Vowpal Wabbit,我通常从终端运行它。 我能够使用Python的调用(vw_命令,shell=True)来运行Vowpal Wabbit,但我还想捕获vw的整个shell输出。 这就是我所尝试的: output =check_output(vw_command, shell=True) 这是我在Python IDE中看到的: creating quadratic features for pairs: h

我正在使用Python自动化我在机器学习中所做的一些工作。我使用的机器学习工具是Vowpal Wabbit,我通常从终端运行它。 我能够使用Python的调用(vw_命令,shell=True)来运行Vowpal Wabbit,但我还想捕获vw的整个shell输出。 这就是我所尝试的:

output =check_output(vw_command, shell=True)
这是我在Python IDE中看到的:

creating quadratic features for pairs: hh hb hp hr  final_regressor = model_vw.vw Num weight bits = 28 learning rate = 0.7 initial_t = 0 power_t = 0.5 decay_learning_rate = 1 creating cache_file = fold1.vw.cache Reading datafile = fold1.vw num sources = 1 average    since         example     example  current  current  current loss      last          counter      weight    label  predict features
0.693147   0.693147            1         1.0  -1.0000   0.0000     1209
0.615108   0.537070            2         2.0  -1.0000  -0.3411     1217
0.527696   0.440284            4         4.0  -1.0000  -0.5287     1217
0.474677   0.421658            8         8.0  -1.0000  -0.9651     1201
0.363831   0.252986           16        16.0  -1.0000  -1.2778     1217
0.259342   0.154852           32        32.0  -1.0000  -2.1993     1209
0.170503   0.081664           64        64.0  -1.0000  -2.5598     1217
0.106260   0.042017          128       128.0  -1.0000  -2.8044     1217
0.083359   0.060457          256       256.0  -1.0000  -8.8870     1217
0.056569   0.029780          512       512.0  -1.0000  -4.4179     1209
0.044556   0.032542         1024      1024.0  -1.0000  -4.0888     1209
0.036272   0.027989         2048      2048.0  -1.0000  -5.3605     1217
0.033533   0.030794         4096      4096.0  -1.0000  -5.7477     1199
0.026593   0.019653         8192      8192.0  -1.0000  -9.1520     1194
0.019215   0.011837        16384     16384.0  -1.0000 -13.8832     1209
0.014147   0.009079        32768     32768.0  -1.0000  -9.5163     1209
0.012523   0.010898        65536     65536.0  -1.0000 -17.4003     1209
0.009721   0.006920       131072    131072.0  -1.0000 -13.1049     1217
0.007038   0.004355       262144    262144.0  -1.0000 -10.1183     1209
0.005340   0.003642       524288    524288.0  -1.0000 -10.7823     1203
0.003909   0.002478      1048576   1048576.0  -1.0000  -9.5997     1209
0.002318   0.000727      2097152   2097152.0  -1.0000 -22.7368     1217

finished run
number of examples per pass = 425000
passes used = 1
weighted example sum = 425000
weighted label sum = -419716
average loss = 236.561
best constant = -0.987572
total feature number = 514554601
但当我查看变量输出时,它是一个空字符串“”。
如何捕获整个shell输出?

将Popen与管道一起使用,如下所示:

import subprocess
child = subprocess.Popen('command',shell=True,stdout=subprocess.PIPE)
output = child.communicate()[0]
print output
你可以使用,这几乎就是魔法。输出直接来自调用:


如果您不想使用其他模块,请忽略我的回答。

我需要Python变量中的输出以进行进一步处理。我将代码更改为:output=child.communicate()[0]。但输出仍然为“”。是的,请尝试打印输出,您将获得值。运气不好。仍然输出=''。您能告诉我您到底给出了什么命令吗???,并尝试使用其他命令进行检查,因为在我的系统中,它与shell命令一起工作正常。Vowpal Wabbit命令是:“vw-d split1-t-i model_vw.vw-p preds_vw.p.txt”。如果我试着用“echo Hello”,效果很好。你能试试生成多行的东西吗?
import subprocess

cmd = "ls -l"

output = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read()

print output