Why isn't the content of `print` displayed immediately in the terminal?

I have a python script to simulate, for

it takes quite a long time for a loop to run and each loop takes a long time to run, so I am typing .

after each loop as a way of monitoring how fast it is running and how far it has gone through a statement for

when the script is run.

for something:
    do something
    print '.',

      

However, when I run the script in iPython in the terminal, the dots are not printed one by one, instead, it prints them all at once when the loop finishes, making everything pointless. How can I print dots per line at startup?

+3


source to share


1 answer


How can I print dots per line at startup?

Try to clear your output, for example:



for _ in range(10):
    print '.',
    sys.stdout.flush()
    time.sleep(.2)  # or other time-consuming work

      

+2


source







All Articles