gpt4 book ai didi

shell - Python,捕获 shell 命令的整个输出

转载 作者:行者123 更新时间:2023-12-02 00:45:33 24 4
gpt4 key购买 nike

我正在使用 Python 来自动化我在机器学习中所做的一些工作。我使用的机器学习工具是 Vowpal Wabbit,我通常从终端运行它。我可以使用 Python 的 call(vw_command,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

关于shell - Python,捕获 shell 命令的整个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26640514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com