倒计时:
import sys print('倒计时程序!') for i in range(10, -1, -1): mystr = '\r倒计时%s秒' % str(i) sys.stdout.write(mystr) sys.stdout.flush() time.sleep(1)
进度条,个人常用方式5
1 先说一下文本系统的控制符: 2 \r: 将光标移动到当前行的首位而不换行; 3 \n: 将光标移动到下一行,并不移动到首位; 4 \r\n: 将光标移动到下一行首位。 5 6 环境: 7 root@ubuntu16:/alex/py/jingdutiao# python3 8 Python 3.5.2 (default, Jul 5 2016, 12:43:10) 9 [GCC 5.4.0 20160609] on linux 10 Type "help", "copyright", "credits" or "license" for more information. 11 >>> 12 13 方式1: 14 root@ubuntu16:/alex/py/jingdutiao# cat test1.py 15 #!/usr/bin/env python 16 from __future__ import division 17 import sys,time 18 j = '#' 19 if __name__ == '__main__': 20 for i in range(1,61): 21 j += '#' 22 sys.stdout.write(str(int((i/60)*100))+'% '+j+'->'+ "\r") 23 sys.stdout.flush() 24 time.sleep(0.5) 25 print 26 27 root@ubuntu16:/alex/py/jingdutiao# python3 test1.py 28 98% ############################################################-> 29 30 31 方式2: 32 root@ubuntu16:/alex/py/jingdutiao# cat test4.py 33 #!/usr/bin/env python 34 # -*- coding:utf-8 -*- 35 __author__ = 'l' 36 37 import sys 38 from time import sleep 39 def viewBar(i): 40 """ 41 进度条效果 42 :param i: 43 :return: 44 """ 45 output = sys.stdout 46 for count in range(0, i + 1): 47 second = 0.1 48 sleep(second) 49 output.write('\rcomplete percent ----->:%.0f%%' % count) 50 output.flush() 51 52 viewBar(100) 53 root@ubuntu16:/alex/py/jingdutiao# python3 test4.py 54 complete percent ----->:100% 55 56 57 方式3: 58 tqdm模块 59 tqdm是一个快速、扩展性强的进度条工具库, 60 其githup地址:https://github.com/tqdm/tqdm 61 62 1)安装: 63 直接使用pip安装: 64 pip install tqdm 65 2)使用: 66 from time import sleep 67 from tqdm import tqdm 68 for i in tqdm(range(1, 500)): 69 sleep(0.01) 70 71 自己实操:在ubuntu上默认安装到2.7环境变量里去了 72 root@ubuntu16:/alex/py/jingdutiao# pip install tqdm 73 Collecting tqdm 74 Downloading tqdm-4.8.4-py2.py3-none-any.whl 75 Installing collected packages: tqdm 76 Successfully installed tqdm-4.8.4 77 You are using pip version 8.1.1, however version 8.1.2 is available. 78 You should consider upgrading via the 'pip install --upgrade pip' command. 79 80 pip install --upgrade pip 81 pip install tqdm 82 pip -V 83 cd /usr/local/lib/python2.7/dist-packages/ 84 cp -r tqdm tqdm-4.8.4.dist-info/ /usr/local/lib/python3.5/dist-packages 85 root@ubuntu16:/alex/py/jingdutiao# cat test5.py 86 from time import sleep 87 from tqdm import tqdm 88 for i in tqdm(range(1, 500)): 89 sleep(0.01) 90 root@ubuntu16:/alex/py/jingdutiao# python3 test5.py 91 100%|████████████████████████████████████████████████████████████████████████| 499/499 [00:05<00:00, 92.20it/s 92 93 94 95 方式4: 96 root@ubuntu16:/alex/py/jingdutiao# cat test2.py 97 98 class ProgressBar(): 99 def __init__(self, width=50): 100 self.pointer = 0 101 self.width = width 102 def __call__(self,x): 103 # x in percent 104 self.pointer = int(self.width*(x/100.0)) 105 return "|" + "#"*self.pointer + "-"*(self.width-self.pointer)+ "|\n %d percent done" % int(x) 106 107 if __name__ == '__main__': 108 import time,os 109 pb = ProgressBar() 110 for i in range(101): 111 os.system('clear') 112 print( pb(i)) 113 time.sleep(0.1) 114 115 root@ubuntu16:/alex/py/jingdutiao# 116 执行结果: 117 |#################################################-| 118 percent done 119 |##################################################| #输出100行内容,但是在屏幕会实时清屏,只展示1行 120 percent done 121 122 123 方式5: 124 root@ubuntu16:/alex/py/jingdutiao# python3 test3.py 125 ====================================================================================================>100% 126 #cat test3.py 127 import sys 128 import time 129 def view_bar(num,total): 130 rate = num / total 131 rate_num = int(rate * 100) 132 #r = '\r %d%%' %(rate_num) #r = '\r%s %d%%' % ('当前进度:' ,rate_num) 133 r = '\r%s>%d%%' % ('=' * rate_num, rate_num,) 134 sys.stdout.write(r) 135 sys.stdout.flush 136 if __name__ == '__main__': 137 for i in range(0, 101): 138 time.sleep(0.1) 139 view_bar(i, 100)
浙公网安备 33010602011771号