1、方法一(显示百分比)
import urllib.request
import sys
import os
def File_plus():
'''
文件储存位置
判断文件是否存在,不存在则创建
'''
folder_path = "C:/Users/Admin/Desktop/download/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
return folder_path
def progress(block_num, block_size, total_size):
'''回调函数
@block_num: 已经下载的数据块
@block_size: 数据块的大小
@total_size: 远程文件的大小
write(): 显示下载进度,"\r"使得光标回到行首
flush(): 刷新缓存区
'''
per = block_num * block_size / total_size * 100.0
sys.stdout.write('\r>> Downloading %.1f%%' % (per))
sys.stdout.flush()
def download():
folder_path = File_plus()
url = 'http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2'
local = url.split('/')[-1]
urllib.request.urlretrieve(url, folder_path+local, progress)
if __name__ == '__main__':
download()
2、方法二(文件名+百分比)
import urllib.request
import sys
import os
url = 'http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2'
local = url.split('/')[-1]
def File_plus():
'''
文件储存位置
判断文件是否存在,不存在则创建
'''
folder_path = "C:/Users/Admin/Desktop/download/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
return folder_path
def progress(block_num, block_size, total_size):
'''回调函数
@block_num: 已经下载的数据块
@block_size: 数据块的大小
@total_size: 远程文件的大小
write(): 显示下载进度,"\r"使得光标回到行首
flush(): 刷新缓存区
'''
sys.stdout.write('\r>> Downloading %s %.1f%%' % (
local, float(block_num * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
def download():
folder_path = File_plus()
urllib.request.urlretrieve(url, folder_path+local, progress)
if __name__ == '__main__':
download()
3、方法三(显示文件大小)
import urllib.request
import sys
import os
def File_plus():
'''
文件储存位置
判断文件是否存在,不存在则创建
'''
folder_path = "C:/Users/Admin/Desktop/download/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
return folder_path
def progress(block_num, block_size, total_size):
'''回调函数
@block_num: 已经下载的数据块
@block_size: 数据块的大小
@total_size: 远程文件的大小
write(): 显示下载进度,"\r"使得光标回到行首
flush(): 刷新缓存区
'''
per = block_num * block_size / total_size * 100.0
downsize = block_num * block_size
Do_size = '\r>> Downloading:%.1f%%' % (per)+" ===> "+"%.2fM" % (
downsize/1024/1024) + "/" + "%.2fM" % (total_size/1024/1024)
sys.stdout.write(Do_size)
sys.stdout.flush()
def download():
folder_path = File_plus()
url = 'http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2'
local = url.split('/')[-1]
urllib.request.urlretrieve(url, folder_path+local, progress)
if __name__ == '__main__':
download()
4、方法四(显示下载进度条)
import urllib.request
import sys
import os
def File_plus():
'''
文件储存位置
判断文件是否存在,不存在则创建
'''
folder_path = "C:/Users/Admin/Desktop/download/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
return folder_path
def format_size(bytes):
'''
字节bytes转化K M G
'''
bytes = float(bytes) # B(比特)
kb = bytes / 1024
if kb >= 1024:
M = kb / 1024
if M >= 1024:
G = M / 1024
return "%.2fG" % (G)
else:
return "%.2fM" % (M)
else:
return "%.2fK" % (kb)
def progress(block_num, block_size, total_size):
'''回调函数
@block_num: 已经下载的数据块
@block_size: 数据块的大小
@total_size: 远程文件的大小
write(): 显示下载进度,"\r"使得光标回到行首
flush(): 刷新缓存区
'''
downsize = block_num * block_size
speed_str = " 下载进度:%s" % format_size(downsize)
Totalsize = "/%.2fM" % (total_size/1024/1024)
# 设置下载进度条
pervent = downsize / total_size
n = round(pervent * 50)
s = ('#' * n).ljust(50, '-')
sys.stdout.write('\r' + speed_str + Totalsize+'[' + s + ']')
sys.stdout.flush()
def download():
folder_path = File_plus()
url = 'http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2'
local = url.split('/')[-1]
urllib.request.urlretrieve(url, folder_path+local, progress)
if __name__ == '__main__':
download()