Python Implementation progress bar

Source: Internet
Author: User

Let's start with the text system's control: \ r: Moves the cursor to the top of the current line without wrapping; \ n: Moves the cursor to the next line, not to the first; \ r \ n: Moves the cursor to the top of the next line. Environment: [email protected]:/alex/py/jingdutiao# Python3python 3.5.2 (default, Jul 5, 12:43:10) [GCC 5.4.0 20160609] O n Linuxtype "Help", "copyright", "credits" or "license" for more information.>>> way 1:[email protected]:/ alex/py/jingdutiao# cat test1.py #!/usr/bin/env pythonfrom __future__ import Divisionimport sys,timej = ' # ' If __name__ = = ' __main__ ': For I in Range (1,61): J + = ' # ' Sys.stdout.write (str (int (I/60) *100) + '% ' +j+ ' + ' + ' \ R ') Sys.stdou T.flush () time.sleep (0.5) print[email protected]:/alex/py/jingdutiao# python3 test1.py 98% #################### ########################################-> mode 2:[email protected]:/alex/py/jingdutiao# cat test4.py #!/usr/ Bin/env python #-*-Coding:utf-8-*-__author__ = ' l ' Import sysfrom time import sleepdef ViewBar (i): "" "progress bar effect:p Aram I:: return: "" "Output = Sys.stdout for count in range (0, I+ 1): Second = 0.1 sleep (second) output.write (' \rcomplete percent----->:%.0f%% '% count) outpu T.flush () ViewBar (+) [email protected]:/alex/py/jingdutiao# python3 test4.py Complete percent----->:100% Mode 3:TQDM Module TQDM is a fast, extensible progress bar Tool Library, its githup address: HTTPS://GITHUB.COM/TQDM/TQDM 1) Installation: Direct use of PIP installation: Pip install tqdm2) using : From time import sleepfrom TQDM import tqdmfor i in TQDM (range (1,)): Sleep (0.01) Self-operation: default installation in Ubuntu to 2.7 environment variables [E mail protected]:/alex/py/jingdutiao# pip install tqdmcollecting tqdm downloading Tqdm-4.8.4-py2.py3-none-any.whlinstalling collected packages:tqdmsuccessfully installed Tqdm-4.8.4You is using PIP Version 8.1.1, however version 8.1.2 is available. You should consider upgrading via the ' pip install--upgrade pip ' command.pip install--upgrade pippip Install TQDMPIP-VC D/usr/local/lib/python2.7/dist-packages/cp-r TQDM tqdm-4.8.4.dist-info//usr/local/lib/python3.5/dist-packages[ email protected]:/alex/py/jingdutiao# Cat test5.py from time import sleepfrom TQDM import tqdmfor i in TQDM (range (1,)): Sleep (0.01) [Email&nbs p;protected]:/alex/py/jingdutiao# Python3 test5.py 100%| ████████████████████████████████████████████████████████████████████████|  499/499 [00:05<00:00, 92.20it/s way 4:[email protected]:/alex/py/jingdutiao# cat test2.py 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 do NE "% int (x) If __name__ = = ' __main__ ': Import TIME,OSPB = ProgressBar () for I in Range (101): Os.system (' clear ') PR Int (PB (i)) Time.sleep (0.1) [email protected]:/alex/py/jingdutiao# execution Result: |####################################   #############-|percent done|##################################################| #输出100行内容, but the screen will clear the screen in real time, showing only 1 rows percent done way 5:[email protected]:/alex/py/jingdutiao# Python3 test3.py ========================================================================= ===========================>100% #cat test3.py Import sysimport timedef View_bar (num,total): rate = Num/total ra te_num = Int (rate *) #r = ' \ r%d%% '% (rate_num) r = ' \r%s>%d%% '% (' = ' * rate_num, rate_num,) SYS.STDOUT.W  Rite (r) Sys.stdout.flushif __name__ = = ' __main__ ': For I in range (0, 101): Time.sleep (0.1) View_bar (I, 100)

  

Python Implementation progress bar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.