下载远程文件, 加入一个进度显示 分类: python 20130609 12:09 314人阅读 评论(0) 收藏

remote = urllib.urlopen(remote_file)
local_file = open(local_path_name, "wb")
local_file.write(remote.read())

问题:

用这种方式可以很好的下载远程文件, 现在需要加入一个进度显示的功能, 类似于下载软件一样, 显示出下载的百分比, 请问怎么做?


实现方式:
>>> def report_hook(count, block_size, total_size):
...     print '%02d%%'%(100.0 * count * block_size/ total_size)
...
>>> urllib.urlretrieve("http://sports.sina.com.cn/", reporthook= report_hook)
00%
01%
03%
...

或:


def my_reporthook(blocknum, blocksize, totalsize, url = None):
 ...

urllib.urlretrieve(remote_file, local_path + eachName, lambda nb, bs, fs, remote_file = remote_file:my_reporthook(nb, bs, fs, remote_file))

原文地址:https://www.cnblogs.com/think1988/p/4628172.html