python《文件下载进度显示》 urllib.request.urlretrieve(self.__path,self.__name,jindu)

#coding=utf-8
import threading
import os,sys
import urllib.request as ur

#显示调用函数
def jindu(a,b,size):
os.system('cls')
per=100*a*b/size
per=round(per, 2)

if per>100:
per=100
sys.stdout.write('下载进度:{0}%\r'.format(per))

sys.stdout.flush()

#继承类多线程
class Doal(threading.Thread):
def __init__(self,a,b):
# 继承类多线程
threading.Thread.__init__(self)
self.__path=a
self.__name=b
def run(self):
#开启实时显示
ur.urlretrieve(self.__path,self.__name,jindu)
if __name__ == '__main__':
#下载地址
urls = 'http://dubapkg.cmcmcdn.com/duba/166/kinst_166_f28_k1541.exe'
#名称
name=urls.split('/')[-1]
#创建文件夹
if not os.path.exists('code'):
os.mkdir('code')
#路径名称链接
filepath = os.path.join('code',name)
#实例
p=Doal(urls,filepath)
#线程开启
p.start()

原文地址:https://www.cnblogs.com/huazhou695/p/9873336.html