我想要的是使用 time.sleep() 打印出每秒打印一个点的 5 个点,但结果是在延迟 5 秒后一次打印了 5 个点。
尝试了 print 和 sys.stdout.write,结果相同。
感谢您的任何建议。
import time
import sys
def wait_for(n):
"""Wait for {n} seconds. {n} should be an integer greater than 0."""
if not isinstance(n, int):
print 'n in wait_for(n) should be an integer.'
return
elif n < 1:
print 'n in wait_for(n) should be greater than 0.'
return
for i in range(0, n):
sys.stdout.write('.')
time.sleep(1)
sys.stdout.write('\n')
def main():
wait_for(5) # FIXME: doesn't work as expected
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print '\nAborted.'
最佳答案
写完需要冲水
sys.stdout.write('foo')
sys.stdout.flush()
wastetime()
sys.stdout.write('bar')
sys.stdout.flush()
关于python - 函数中带有 time.sleep() 的 sys.stdout.write() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2808832/