Does "time.sleep()" not work inside a for loop with a print function using the "end" attribute?

Question:

So, I’m just recently learning python and I was playing with some code. I wanted to print the some character without line breaks over a loop with some delay. I used the time.sleep() function inside the for loop. But, all it does is delay the output for the total time it would have taken in the loop, all at once and, then print out the character.

I did try it without the “end” attribute and it worked perfectly. But, I didn’t want the line break.

from time import sleep
print("starting the progress bar")


for i in range(50):
    sleep(0.1)
    print("#", end = '')

I expected the output to print a character and with a delay, print another character. But, the script delays for 0.1 seconds for 50 times and then prints out all the characters at once

Asked By: thesuzan

||

Answers:

As python is linebuffered it will wait for a newline before printing the stdout.

Solution 1:

Add PYTHONUNBUFFERED=1 to your env.var:

export PYTHONUNBUFFERED=1

This will allow the output to be immediately dumped

Solution 2:

As you are using python >= 3 you can use the flush=True

for i in range(50):
    sleep(0.1)
    print("#", end="", flush=True)
Answered By: Nic Laforge

By default, Python is linebuffered. As long as you print without a newline, output is collected but not shown. You must forcefully flush the output.

from time import sleep
print("starting the progress bar")


for i in range(50):
    sleep(0.1)
    print("#", end = '', flush=True)

Note in that whatever you use to view the output might be linebuffered as well. This cannot be changed from within your script.

Answered By: MisterMiyagi

I just found a solution on reddit.

reddit comment on why it doesn’t work and how beginners fall into the same pitfall

So, it has something to do with buffering.

Here’s the code that would work;

from time import sleep
print("starting the progress bar")


for i in range(50):
    sleep(0.1)
    print("#", end = '', flush = True)
Answered By: thesuzan

You can use the -u option when running your program.

$ man python3


PYTHON(1)                                                            PYTHON(1)

...

       -u     Force  the  stdout  and  stderr  streams to be unbuffered.  This
              option has no effect on the stdin stream.

Run like this: python3 -u file.py


Alternatively, you can set the PYTHONUNBUFFERED environment variable in your shell

       PYTHONUNBUFFERED
              If this is set to a non-empty string it is equivalent to  speci-
              fying the -u option.

Like so: PYTHONUNBUFFERED="yes" python3 file.py


Lastly, you can use flush=True as other answers have mentioned.

Answered By: xrisk
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.