python `print` does not work in loop
Asked Answered
B

3

16

I have multi loops in together and a sleep in the most inner loop. for example:

from time import sleep

for i in range(10):
    print i,
    for j in range(-5,5):
        if j > 0:
            print '.',
        else:
            print 'D',
        sleep(1)
    print ''

if you run the code, you may expected to get i value after it D sleep 1 second and another D and again sleep until to the end.

but the result is difference, it waits 10 seconds and prints the whole line of 0 D D D D D D . . . . and waiting again to printing next line.

I found the comma at the end of printing causes this problem. How can I solve it?

Bowhead answered 18/8, 2014 at 17:25 Comment(2)
what output do you expect, it prints a new line on each iteration for meGrogshop
every j iterate? it prints on every i iteration but I need prints on every jBowhead
B
17

Because of existence of comma, the output buffers until a \n.

You should flush the stdout after every print or use sys.stdout.write and flush buffer.

Define your print method:

import sys

def my_print(text):
    sys.stdout.write(str(text))
    sys.stdout.flush()

and at the end of line print a \n

Bowhead answered 18/8, 2014 at 17:25 Comment(1)
it did not work with me, I tried flushing the output but nothing happensThanh
B
3

The problem using print <something>, is buffering and printing only when the result is ready to be printed.

You can solve it using print_function from __future__ (which will be in compliance with Python 3 as well):

from __future__ import print_function
from time import sleep
import sys

for i in range(10):
    print(i, end='')
    for j in range(-5,5):
        if j > 0:
            print('.', end='')
        else:
            print('D', end='')
        sys.stdout.flush()
        sleep(1)            
    print('')
Bullis answered 18/8, 2014 at 17:32 Comment(3)
You still need a sys.stdout.flush() to make it look like it's printing incrementally. If using Python 3.3+, then supplying a flush=True to print is also available.Broucek
@JonClements thanks I wasn't aware of it since it works fine without the flush in python 2.7.5Bullis
It's sys.stdout that does the buffering. Using print() as a function does not change the buffering behaviour, but you can use flush=True in newer Python versions to force a flush. Use sys.stdout.flush() on versions where the flush argument is not available (like Python 2).Hexateuch
T
0

I would suggest override print just after import with below coe

import sys
def print_msg(*args,end='\n'):
    for item in args:
        sys.stdout.write(str(item)+' ')
    sys.stdout.write(end)
    sys.stdout.flush()

print = print_msg
Tremolant answered 31/7, 2019 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.