Python简单进度条示例
2015-11-17 18:00
597 查看
最近需要用Python写一个小脚本,用到了一些小知识,赶紧抽空记录一下。不深但是常用。
两个进度条示例,拷贝就能运行:
提供一个自己写的一个简单异步进度条,可以在耗时操作前开启,然后再耗时操作结束后停止。
用法:
两个进度条示例,拷贝就能运行:
# coding=utf-8 import sys import time # width:宽度, percent:百分比 def progress(width, percent): print "\r%s %d%%" % (('%%-%ds' % width) % (width * percent / 100 * '='), percent), if percent >= 100: print sys.stdout.flush() # 示例一、0%--100% def demo1(): for i in xrange(100): progress(50, (i + 1)) time.sleep(0.1) ## 示例二、周期加载 def demo2(): i = 19 n = 200 while n > 0: print "\t\t\t%s \r" % (i * "="), i = (i + 1) % 20 time.sleep(0.1) n -= 1 demo1() demo2()
提供一个自己写的一个简单异步进度条,可以在耗时操作前开启,然后再耗时操作结束后停止。
import time import thread import sys class Progress: def __init__(self): self._flag = False def timer(self): i = 19 while self._flag: print "\t\t\t%s \r" % (i * "="), sys.stdout.flush() i = (i + 1) % 20 time.sleep(0.05) print "\t\t\t%s\n" % (19 * "="), thread.exit_thread() def start(self): self._flag = True thread.start_new_thread(self.timer, ()) def stop(self): self._flag = False time.sleep(1)
用法:
progress = Progress() progress.start() time.sleep(5) progress.stop()
相关文章推荐
- Python编写微信打飞机小游戏(九)
- 将Python脚本打包成可执行文件
- python装饰器decorator之计算函数运行时间的例子
- 【Python】Python点滴(一)——pip工具使用
- python if __name__ == '__main__'解析
- 利用Python实现从百度下载图片到本地磁盘
- Python多进程不要使用TimedRotatingFileHandler
- 测试开发Python培训:模拟登录新浪微博-技术篇
- Scrapy爬取天天美剧封面照及剧集下载地址 推荐
- Python基础学习-小代码1【爬取网页图片】
- 测试开发Python培训:抓取新浪微博抓取数据-技术篇
- 测试开发Python培训:自动发布新浪微博-技术篇
- python自省函数的总结(源码剖析)
- Python3下周百度页面实例
- Python模拟登陆(urllib2)
- python与POP3
- 构建Python+Selenium2自动化测试环境
- Python 多进程日志记录
- ubuntu下python程序中有汉字,添加环境变量
- python编写守护进程