Why is my python output delayed to the end of the program?
Question: I've got an extremely simple application:

    
    
    import sys
    from time import sleep
    
    for i in range(3):
        sys.stdout.write('.')
        sleep(1)
    
    print('Welcome!')
    

I expect it to print out a dot every second (3 times), after which it should
display "Welcome!". Unfortunately, it simply waits three seconds, and then
prints out everything at once. I'm on a mac running regular Python 2.7 and I
have no clue why this code behaves like this. Any suggestions?


Answer: It's because `sys.stdout` is buffered. Use `flush`:

    
    
    import sys
    from time import sleep
    
    for i in range(3):
        sys.stdout.write('.')
        sys.stdout.flush()
        sleep(1)
    
    print('Welcome!')
    

