如何在Jupyter Notebook中运行系统命令并获得连续输出?

时间:2019-07-02 15:24:18

标签: python console jupyter-notebook outputstream

如果我在Jupyter Notebook中运行系统命令,我希望控制台应用程序的日志消息在笔记本中“立即”显示。但是,它们似乎仅在完成整个过程之后才会发生。

我尝试过

!D:/long_running_executable_with_log_messages.exe

import os
os.system('D:/long_running_executable_with_log_messages.exe')

=>如何连续获取系统命令的输出?

相关问题:

Running interactive command line code from Jupyter notebook

1 个答案:

答案 0 :(得分:0)

from subprocess import Popen, PIPE, CalledProcessError

with Popen(cmd, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
    for line in p.stdout:
        print(line, end='') # process line here

if p.returncode != 0:
    raise CalledProcessError(p.returncode, p.args)

位于Constantly print Subprocess output while process is running