【问题标题】:Seeing the progress of my code in real time in jupyter在jupyter中实时查看我的代码进度
【发布时间】:2023-02-22 04:03:06
【问题描述】:

我有在 jupyter notebook 上运行的代码

with open('tracker.txt', 'w+') as p:
    for i in range(1,100000000):
        p.write("\nValue is: "+str(i) )

当我打开 tracker.txt 文件时运行此代码时,它显示我的空白,并且仅在代码完全执行后才显示结果。但我想看到实时打印在文件中的结果,以便我可以跟踪代码的进度。我不知道如何实现这一目标。任何帮助都会很棒。

【问题讨论】:

  • 在循环内的p.write(...) 之后调用p.flush()
  • 获取输出文件的副本。它会告诉你东西在哪里。
  • @Stef thaaanks.. 它起作用了.. :)

标签: python jupyter


【解决方案1】:

在循环内的p.write(...) 之后调用p.flush()

with open('tracker.txt', 'w+') as p:
    for i in range(1,100000000):
        p.write("
Value is: "+str(i) )
        p.flush()

另见:How often does python flush to a file?

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2023-01-25
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多