What is the difference between the progress bar and the general print?
The answer is that print will output a \ n, a newline character, so that the cursor moves to the beginning of the next line, then the output, something that was previously output through the stdout remains, and we see the latest output below.
Progress bar Otherwise, we must re-in-situ output to ensure that he is a progress bar, or change the line how to still call the progress bar?
The simplest way is, after the output is finished, the cursor moves to the beginning of the line, continue to be there to output a longer progress bar can be achieved, the new longer progress bar to the old short cover, the formation of animation effect.
Can think of that escape character, that is \ r.
Escape character \ r to move the cursor to the beginning of the line without wrapping, the escape character \ n Moves the cursor to the beginning of the line and wraps it.
In Python, the output stdout (standard output) can be used Sys.stdout.write
For example:
#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v2#Author: [email protected]##2010 -10-27 22:07"""usage:just A Template""" from __future__ ImportDivisionImportSys,timej='#'if __name__=='__main__': forIinchRange (1,61): J+='#'sys.stdout.write (str (int (i/60) (*100)) +'% ||'+j+' -'+"\ r") Sys.stdout.flush () Time.sleep (0.5)Print
The second way of thinking is to use the escape character \b
Escape character \b is the backspace key, that is, the output of the cursor back to the grid, so that you can not use + =, for example:
#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v2#Author: [email protected]#2010-10-27 22:07"""usage:just A Template""" from __future__ ImportDivisionImportSys,timeif __name__=='__main__': forIinchRange (1,61): Sys.stdout.write ('#'+' -'+"\b\b") Sys.stdout.flush () Time.sleep (0.5)Print
The cursor back 2, write a # back, then write, to achieve the purpose of growth
But to write so much seems to be nonsense, in the ear often hear a word: that is, do not repeat the wheel. In fact Python has a rich set of LIB to help you implement this thing, you can put your mind on logic development without paying attention to these small details
The following is to introduce this class "ProgressBar" (http://code.google.com/p/python-progressbar/), using Easy_install can easily install this can be a class library, in fact, a file, Put it in the same directory under the file and you can import it directly.
:
Here is an example of basic use:
#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v2#Author: [email protected]#2010-10-27 22:53"""usage:just A Template""" from __future__ ImportDivisionImportSys,time fromProgressBarImport* Total= 1000#Basic UsageProgress =ProgressBar () forIinchProgress (Range): Time.sleep (0.01) Pbar=ProgressBar (). Start () forIinchRange (1,1000): pbar.update (int ((I/(TOTAL-1)) *100)) Time.sleep (0.01) pbar.finish ()#Advanced UsageWidgets = ['Progress:', Percentage (),' ', Bar (Marker=rotatingmarker ('>-=')), ' ', ETA (),' ', Filetransferspeed ()]pbar= ProgressBar (Widgets=widgets, maxval=10000000). Start () forIinchRange (1000000): #Do somethingPbar.update (10*i+1) Time.sleep (0.0001) pbar.finish ()
Official sample Download, Shift here: http://code.google.com/p/python-progressbar/source/browse/progressbar/examples.py
To send a second class:
#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v2#Author: [email protected]#2010-10-30 13:59"""usage:just A Template"""classProgressbarclass:def __init__(Self, finalcount, progresschar=None):ImportSYS Self.finalcount=Finalcount Self.blockcount=0# #See If caller passed me a character #progress bar (like "*"). If not use the block #character that makes it look like a real progress #Bar. # if notPROGRESSCHAR:SELF.BLOCK=CHR (178) Else: self.block=Progresschar# #Get Pointer to sys.stdout so I can use the Write/flush #methods to display the progress bar. #self.f=Sys.stdout# #If The final count is zero, don ' t start the progress gauge # if notSelf.finalcount:returnSelf.f.write ('\ n-------------------% Progress-------------------\ n') return defProgress (self, count):# #Make sure I don ' t try to go off the end (e.g. >100%) #Count=min (count, Self.finalcount)# #If Finalcount is zero, I ' m do # ifSelf.finalcount:percentcomplete=int (Round (100*count/self.finalcount))ifPercentComplete < 1:percentcomplete=1Else: PercentComplete=100#print "percentcomplete=", PercentCompleteBlockcount=int (PERCENTCOMPLETE/2) #print "blockcount=", Blockcount ifBlockcount >Self.blockcount: forIinchRange (Self.blockcount,blockcount): Self.f.write (Self.block) Self.f.flush () ifPercentComplete = = 100:self.f.write ("\ n") Self.blockcount=Blockcountreturn if __name__=="__main__": fromTimeImportSleep PB=progressbarclass (8,"*") Count=0 whileCount<9: Count+=1pb.progress (count) sleep (0.2)
Reprinted from: http://blog.ihipop.info/2010/10/1736.html
Python: How to display a progress bar