【问题标题】:Simultaneously displaying and capturing standard out in IPython?在 IPython 中同时显示和捕获标准输出?
【发布时间】:2014-01-08 03:12:10
【问题描述】:

我有兴趣在 IPython 中实现类似于 !!! 的组合的行为。我正在尝试使用 IPython 终端作为我的 (Windows) shell 的附件。对于长时间运行的命令(例如,构建脚本),我希望能够像! 那样观看输出。我还想像!! 那样将命令的输出捕获到输出历史记录中,但这会推迟打印任何内容,直到所有输出都可用。

有没有人对如何实现这样的事情有任何建议?我猜IPython.utils.io.Tee() 对象在这里会很有用,但我对 IPython 的了解还不够,无法正确连接。

【问题讨论】:

    标签: ipython


    【解决方案1】:

    这是我刚刚在 iPython notebook v2.3 中尝试过的一段代码,它似乎符合要求:

    import sys
    import IPython.utils.io
    outputstream = IPython.utils.io.Tee("outputfile.log", "w", channel="stdout")
    outputstream.write("Hello worlds!\n")
    outputstream.close()
    
    logstream=open("outputfile.log", "r")
    sys.stdout.write("Read back from log file:\n")
    sys.stdout.write(logstream.read())
    

    日志文件创建在与 iPython 笔记本文件相同的目录中,运行此单元的输出显示如下:

    Hello worlds!
    Read back from log file:
    Hello worlds!
    

    我没有在 iPython 终端中尝试过这个,但看不出它在那里不能正常工作的原因。

    (作为牛津参与http://aaronswartzhackathon.org 的一部分进行了研究和回答)

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多