In Python, why won't something print without a newline?

后端 未结 6 2348
轮回少年
轮回少年 2020-12-17 15:53
import time
import sys
sys.stdout.write(\"1\")
time.sleep(5)
print(\"2\")

will print \"12\" after 5 seconds

import time
import sys
         


        
6条回答
  •  旧巷少年郎
    2020-12-17 16:17

    Buffering. It's not really Python, but rather your operating system/terminal. Output from any program is sent to a buffer, a holding area of memory. When a whole line is collected, it's sent to the screen. There is usually a hook, a method named something like flush(), to force output of partial lines.

提交回复
热议问题