9

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

17
  • Are you sure your print statement is not outside of the for loop? Be careful with indentation Commented Jul 5, 2019 at 4:58
  • @NicLaforge Yes, there are no indentation mistakes in the code. Commented Jul 5, 2019 at 5:02
  • Not sure why @tawab_shakeel modified your code in the question, as he edited with the proper answer. I have looked at your original code and your print was outside the loop Commented Jul 5, 2019 at 5:02
  • @NicLaforge i just put the code in code block otherwise the question was not in a readable format Commented Jul 5, 2019 at 5:04
  • @thesuzan you have option not to accept the edit if you feel the edit is not properly done by any other user Commented Jul 5, 2019 at 5:06

5 Answers 5

12

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) 
Sign up to request clarification or add additional context in comments.

8 Comments

This doesn't change anything. I get the same result; the delay first, and then the characters are printed, without any delay in between.
Are you running from terminal?
I'm using a linux subsystem on windows to run the code with Python 3.6.7. But, I also tried it on the anaconda prompt with Python 3.7.3. Both gave me the same results
I don't understand that. Do I add "export PYTHONUNBUFFERED=1" at the beginning of my code? That gives me an error.
No run it from the terminal before calling your code. Or even better set it in your bashrc file so it remains
|
1

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) 

1 Comment

or you can use python -u. This also forces output to be unbuffered.
1

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.

2 Comments

This will produce the same result. It will only print at the end.
@NicLaforge I had missed that requirement. Try the new version.
1

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.

Comments

-1

I had the same problem. Just add import time above the code. I was coding a count down system-

import time for i in range(10,0,-1): print(i) time.sleep(1) print("yayy! the code works") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.