[英]Why doesn't time.sleep work as with print end arguments?
I am trying to use time.sleep() to pause in between print statements.我正在尝试使用 time.sleep() 在打印语句之间暂停。
import time
def test():
print("something", end="")
time.sleep(1)
print(" and ", end="")
time.sleep(1)
print("something")
When I use the end argument, the code waits before it starts to print.当我使用 end 参数时,代码会在开始打印之前等待。 It should print, wait, print, wait, and print ("something, wait, and, wait, something").它应该打印、等待、打印、等待和打印(“某事,等等,等等,某事”)。 However, when I use the end argument, it waits, and prints "wait something and something".但是,当我使用 end 参数时,它会等待并打印“等待某事和某事”。 This code works as I wanted it to without the end argumets.这段代码在没有结束参数的情况下按我想要的方式工作。
Your output device is line-buffered and only flushes when a new line is output.您的 output 设备是行缓冲的,并且仅在新行为 output 时刷新。 Add flush=True
as an additional parameter to print
to force a flush to the output.添加flush=True
作为附加参数以强制刷新到print
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.