2

I want to have various terminal printouts while also having a progress bar displayed at the same place on the terminal (say, the bottom) and I want this to work in Python 2 and 3. The code I have now is as follows:

#!/usr/bin/env python

import sys
import time

maximumValue = 10
for i in range(0, maximumValue):
    #print("my count number: {count}".format(count = i))
    sys.stdout.write("\r[" + i * "=" + ">" + (maximumValue - i - 1) * " " + "]")
    sys.stdout.flush()
    time.sleep(0.5)

sys.stdout.write("\n")

The progress bar prints out reasonably here, but when I include the print statement, the progress bar gets printed such that it appears multiple times in the terminal. How can I get a single progress bar to appear while also printing terminal outputs?

d3pd
  • 7,935
  • 24
  • 76
  • 127
  • maybe overkill but you could use the `curses` module; [here](http://blog.skeltonnetworks.com/2010/03/python-curses-custom-progress-bar/) is an example. or this https://code.google.com/p/python-progressbar/ ? – hiro protagonist Jun 07 '15 at 12:41

1 Answers1

0

On Windows, you can try the Console module, as described in this answer, combined with an ASCII progress bar like the one in your question. You mentioned the problem that the progress bar itself appears multiple times on your terminal; the answers to this question generally cover that. For instance, you could try aviraldg's progress bar with the Console module:

import Console
c = Console.getconsole()
progress = 50
c.text(0, -1, "\r[{0}] {1}%".format('#'*(progress/10), progress))
Community
  • 1
  • 1
APerson
  • 8,140
  • 8
  • 35
  • 49