python下载文件时显示下载进度的方法

yipeiwu_com6年前Python基础

本文实例讲述了python下载文件时显示下载进度的方法。分享给大家供大家参考。具体分析如下:

将这段代码放入你的脚本中,类似:urllib.urlretrieve(getFile, saveFile, reporthook=report)

第三个参数如下面的函数定义report,urlretrieve下载文件时会实时回调report函数,显示下载进度

def report(count, blockSize, totalSize):
  percent = int(count*blockSize*100/totalSize)
  sys.stdout.write("\r%d%%" % percent + ' complete')
  sys.stdout.flush()
sys.stdout.write('\rFetching ' + name + '...\n')
urllib.urlretrieve(getFile, saveFile, reporthook=report)
sys.stdout.write("\rDownload complete, saved as %s" % (fileName) + '\n\n')
sys.stdout.flush()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python中os和sys模块的区别与常用方法总结

前言 本文主要介绍了关于python中os和sys模块区别与常用方法的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 官方解释: os: This modu...

python实现代码统计器

本文实例为大家分享了python中的代码行数统计,供大家参考,具体内容如下 思路:统计文件中代码的总行数减去空行单行注释以及多行注释 功能: 1.获取文件内容的总行数 2.排除空行 单行...

举例讲解Django中数据模型访问外键值的方法

先设置一个关于书本(book)的数据模型: from django.db import models class Publisher(models.Model): name...

python base64 decode incorrect padding错误解决方法

python的base64.decodestring方法做base64解码时报错: 复制代码 代码如下: Traceback (most recent call last):  ...

python中如何使用insert函数

这篇文章主要介绍了python中如何使用insert函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 描述 insert() 函数用...