python小功能 模拟下载进度条打印
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import time
def progress(arg):
if arg > 1:
arg = 1
# 设计进度条和百分比
image = int(50 * arg) * '>'
percent = str(int(arg * 100)) + '%'
# 打印进度条
print('\r[%-50s] %s' % (image, percent), end='')
# 下载完成判断并提示
if arg == 1:
print('\nDownload completely!')
recv_size = 0 # 下载文件大小
total_size = 666666 # 文件总大小
while recv_size < total_size:
# 模拟下载速度
time.sleep(0.01)
recv_size += 2048
# 计算比例
ratio = recv_size / total_size
progress(ratio)
运行结果
[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 100%
Download completely!