先说一下文本系统的控制符:
\r: 将光标移动到当前行的首位而不换行;
\n: 将光标移动到下一行,并不移动到首位;
\r\n: 将光标移动到下一行首位。 环境:
root@ubuntu16:/alex/py/jingdutiao# python3
Python 3.5.2 (default, Jul 5 2016, 12:43:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 方式1:
root@ubuntu16:/alex/py/jingdutiao# cat test1.py
#!/usr/bin/env python
from __future__ import division
import sys,time
j = '#'
if __name__ == '__main__':
for i in range(1,61):
j += '#'
sys.stdout.write(str(int((i/60)*100))+'% '+j+'->'+ "\r")
sys.stdout.flush()
time.sleep(0.5)
print root@ubuntu16:/alex/py/jingdutiao# python3 test1.py
98% ############################################################-> 方式2:
root@ubuntu16:/alex/py/jingdutiao# cat test4.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'l' import sys
from time import sleep
def viewBar(i):
"""
进度条效果
:param i:
:return:
"""
output = sys.stdout
for count in range(0, i + 1):
second = 0.1
sleep(second)
output.write('\rcomplete percent ----->:%.0f%%' % count)
output.flush() viewBar(100)
root@ubuntu16:/alex/py/jingdutiao# python3 test4.py
complete percent ----->:100% 方式3:
tqdm模块
tqdm是一个快速、扩展性强的进度条工具库,
其githup地址:https://github.com/tqdm/tqdm 1)安装:
直接使用pip安装:
pip install tqdm
2)使用:
from time import sleep
from tqdm import tqdm
for i in tqdm(range(1, 500)):
sleep(0.01) 自己实操:在ubuntu上默认安装到2.7环境变量里去了
root@ubuntu16:/alex/py/jingdutiao# pip install tqdm
Collecting tqdm
Downloading tqdm-4.8.4-py2.py3-none-any.whl
Installing collected packages: tqdm
Successfully installed tqdm-4.8.4
You are 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 pip
pip install tqdm
pip -V
cd /usr/local/lib/python2.7/dist-packages/
cp -r tqdm tqdm-4.8.4.dist-info/ /usr/local/lib/python3.5/dist-packages
root@ubuntu16:/alex/py/jingdutiao# cat test5.py
from time import sleep
from tqdm import tqdm
for i in tqdm(range(1, 500)):
sleep(0.01)
root@ubuntu16:/alex/py/jingdutiao# python3 test5.py
100%|████████████████████████████████████████████████████████████████████████| 499/499 [00:05<00:00, 92.20it/s 方式4:
root@ubuntu16:/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 done" % int(x) if __name__ == '__main__':
import time,os
pb = ProgressBar()
for i in range(101):
os.system('clear')
print( pb(i))
time.sleep(0.1) root@ubuntu16:/alex/py/jingdutiao#
执行结果:
|#################################################-|
percent done
|##################################################| #输出100行内容,但是在屏幕会实时清屏,只展示1行
percent done 方式5:
root@ubuntu16:/alex/py/jingdutiao# python3 test3.py
====================================================================================================>100%
#cat test3.py
import sys
import time
def view_bar(num,total):
rate = num / total
rate_num = int(rate * 100)
#r = '\r %d%%' %(rate_num)
r = '\r%s>%d%%' % ('=' * rate_num, rate_num,)
sys.stdout.write(r)
sys.stdout.flush
if __name__ == '__main__':
for i in range(0, 101):
time.sleep(0.1)
view_bar(i, 100)

  

最新文章

  1. GDB调试命令小结
  2. python des ecb 加密 demo
  3. 关于zero_interconnect_delay_mode和nonzero_interconnect_delay_mode的区别
  4. python 随笔(property &amp; __class__)
  5. 怎么定义 logger
  6. linux下安装php的imagick扩展模块(附php升级脚本)
  7. bzoj 1791: [Ioi2008]Island 岛屿
  8. Unity Js与C#脚本通信
  9. (六)6.11 Neurons Networks implements of self-taught learning
  10. Codeforces Gym 100523K K - Cross Spider 计算几何,判断是否n点共面
  11. iOS中的定时器 - NSTimer 使用方法
  12. matlab操作之--读取指定文件夹下的“指定格式”文件
  13. 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用
  14. struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)
  15. JVM常用配置参数说明
  16. 文件IO流
  17. 四则运算 来源:一位热心的网友 http://www.tqcto.com/article/software/336297.html
  18. 国内maven仓库地址资源汇总
  19. Numpy中扁平化函数ravel()和flatten()的区别
  20. 深入理解 JavaScript(五)

热门文章

  1. How many ways(记忆化搜索)
  2. Ultra-QuickSort(归并排序)
  3. HP DL360 G7通过iLO部署系统
  4. SPOJ QTREE4 lct
  5. select2简单例子
  6. U盘安装CentOS6.x报错:Missing ISO 9660 Image
  7. UISearchBar总结
  8. JDK源码学习--String篇(三) 存储篇
  9. C++ template学习二 类模板定义及实例化
  10. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】