0

This is the best I've got, but it doesn't work if I want it to appear on a screen with other information:

timer = 0
loading = "Loading: [----------]"

while timer < 11:
    os.system('clear')
    print loading
    loading = loading.replace("-","=",1)
    time.sleep(1)
    timer += 1
time.sleep(1)
os.system('clear')
print loading+" Complete!"

I would prefer a method that works on both windows and linux, not requiring a module that doesn't come stock (So I can give others this program who have windows or linux and have it work fine)

If not that's still fine. I can always have a "Dependencies" list.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Axiom
  • 902
  • 1
  • 10
  • 23
  • 1
    Have you looked at [_Text Progress Bar in the Console_](http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console)? – martineau Nov 18 '14 at 16:53
  • Perfect! I had to alter it a bit to work with string but works. Thanks :) – Axiom Nov 18 '14 at 17:02

2 Answers2

1

The trick is to skip writing a new line at the end of the message and to use backspaces to clear for the next round. Use sys.stdout with a flush to skip the new line, and as many backspaces as the message to clear:

import os
import sys
import time

timer = 0
loading = "Loading: [----------]"
backtrack = '\b'*len(loading)

while timer < 11:
    sys.stdout.write(backtrack + loading)
    sys.stdout.flush()
    loading = loading.replace("-","=",1)
    time.sleep(1)
    timer += 1
time.sleep(1)
sys.stdout.write(backtrack)
print loading+" Complete!"
tdelaney
  • 73,364
  • 6
  • 83
  • 116
-1

So, thanks to martineau's comment, I was able to do this:

import time,sys
loading = "Loading: [----------]"
for i in range(101):
    time.sleep(0.1)
    sys.stdout.write("\r"+loading+" %d%%" % i)
    if i == 10 or i == 20 or i == 30 or i == 40 or i == 50 or i == 60 or i == 70 or i == 80 or i == 90 or i == 100:
        loading = loading.replace("-","=",1)
    sys.stdout.flush()
Axiom
  • 902
  • 1
  • 10
  • 23
  • "Every single progress bar gets printed one after another" Uh no it doesn't? It works perfectly fine for me. [Screenshot](http://prntscr.com/57oe62) – Axiom Nov 18 '14 at 17:35