函数中带有time.sleep()的sys.stdout.write()问题

phi*_*kim 5 python buffering

我想要的是使用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.'
Run Code Online (Sandbox Code Playgroud)

Ign*_*ams 8

写完后你需要冲洗.

sys.stdout.write('foo')
sys.stdout.flush()
wastetime()
sys.stdout.write('bar')
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)