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

yipeiwu_com5年前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持久化文件读写

持久化文件读写: f=open('info.txt','a+') f.seek(0) str1=f.read() if len(str1)==0: f1 = open('info...

详细介绍pandas的DataFrame的append方法使用

详细介绍pandas的DataFrame的append方法使用

官方文档介绍链接:append方法介绍 DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=...

Python的Flask框架中SQLAlchemy使用时的乱码问题解决

一、问题 这两天在学习使用flask + SQLAlchemy 定制一个web查询页面的demo ,在测试时,发现查询到的结果显示乱码 。这里将解决方法记录下。 二、解决思路 1、fla...

numpy找出array中的最大值,最小值实例

在python中利用numpy创建一个array, 然后我们想获取array的最大值,最小值。可以使用一下方法: 一、创建数组 这样就可以获得一个array的最大值和最小值了。 并且可以...

利用python在excel里面直接使用sql函数的方法

利用python在excel里面直接使用sql函数的方法

我们一般在Excel里面是使用数据连接属性里面写sql语句,或者vba里面利用ado组件执行sql语句。 新版的Excel里面带上了Power query的功能也可以使用Odbc.Dat...