How to flush output of print function?

后端 未结 13 1567
旧时难觅i
旧时难觅i 2020-11-21 05:15

How do I force Python\'s print function to output to the screen?

This is not a duplicate of Disable output buffering - the linked question is attempting unbuffe

13条回答
  •  遥遥无期
    2020-11-21 05:42

    On Python 3, print can take an optional flush argument

    print("Hello world!", flush=True)
    

    On Python 2 you'll have to do

    import sys
    sys.stdout.flush()
    

    after calling print. By default, print prints to sys.stdout (see the documentation for more about file objects).

提交回复
热议问题