How to output a progress bar in the python Console
This article describes how to output a progress bar in the python console. Share it with you for your reference. The specific implementation method is as follows:
The progress bar is as follows:
?
1 2 |
| ########################### ----------------- | 59 percent done |
The Code is as follows:
?
1 2 3 4 5 6 7 8 9 |
Class ProgressBar (): Def _ init _ (self, width = 50 ): Self. pointer = 0 Self. width = width Def _ call _ (self, x ): # X in percent Self. pointer = int (self. width * (x/100.0 )) Return "|" + "#" * self. pointer + "-" * (self. width-self.pointer) + \ "| \ N % d percent done" % int (x) |
Test function (for windows system, change "clear" into "CLS "):
?
1 2 3 4 5 6 7 |
If _ name _ = '_ main __': Import time, OS Pb = ProgressBar () For I in range (101 ): OS. system ('clear ') Print pb (I) Time. sleep (0.1) |
I hope this article will help you with Python programming.