0

I have been researching this question, and it seems that it is possible, but not possible on Windows.

I am trying to achieve a loading bar that will print 'Loading', then 'Loading.', 'Loading..'... etc.

This code seems to work on Linux but not Windows(Python 3.5):

x = 0
for x in range (0,5):
    #x = x + 1
    b = "Loading" + "." * x
    print (b, end="\r")
    time.sleep(1)

How do I combat this problem?

Benjamin Wall
  • 51
  • 1
  • 9
  • Possible duplicate of [output to the same line overwriting previous output ? python (2.5)](http://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output-python-2-5) I'm pretty sure you're using 2.x, which means you need to use the `from __future__` statement in the first answer there. – jpmc26 Aug 03 '16 at 16:16
  • @jmpc - looks like they're having a problem actually getting that to work – Wayne Werner Aug 03 '16 at 16:16
  • 1
    What output do you get instead? Also, *this* code won't work on any platform. You need to copy the code from your editor. – Wayne Werner Aug 03 '16 at 16:17
  • 1
    @jpmc26: seems to me the OP is trying to follow the accepted answer. Try putting a `\r` at the start of the string. – cdarke Aug 03 '16 at 16:17
  • 1
    If you don't mind 3rd party dependencies, Click has some [progress bar helpers](http://click.pocoo.org/5/utils/#showing-progress-bars) – Wayne Werner Aug 03 '16 at 16:40
  • I am currently using python 3.5 @jpmc26 I get the output of LoadingLoading.Loading..Loading... I do believe. – Benjamin Wall Aug 04 '16 at 21:05
  • @Wayne Werner I get the output of LoadingLoading.Loading..Loading... – Benjamin Wall Aug 04 '16 at 21:07

1 Answers1

2

The problem is buffering: everything appears at once when the script ends. You can force Python >3.3 to flush after each print output:

import time
print("Loading", end="", flush=True)
for x in range(0,5):
  print(".", end="", flush=True)
  time.sleep(.2)

Of course a loading bar may look cool, but a fake one that just wastes time sleeping is not going to make the user happy. If your program really is doing business, have a look at threading or subprocess.


Your original idea works for me (Python 3.4 on Win7 64 bit), too:
import time
for x in range(0,5):
  print("Loading" + "."*x, end="\r", flush=True)
  time.sleep(.2)
handle
  • 5,063
  • 2
  • 40
  • 69
  • Yes, it was just a waste if time at the start of a program to make it look better - it was just a 'robot' booting up. – Benjamin Wall Aug 04 '16 at 21:12
  • @Benjaminwall So did this answer your question / work for you? Please don't forget to accept an answer. – handle Aug 09 '16 at 07:42
  • Other solutions: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console – handle Aug 19 '16 at 11:46
  • I know that this is three years on, but just the other day was searching for this again and came to my own question. Running your code now just prints the lines without the newline character, so not too helpful. – Benjamin Wall Oct 09 '19 at 19:17