python 蟒蛇下载大型文件显示进度条

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 蟒蛇下载大型文件显示进度条相关的知识,希望对你有一定的参考价值。

try:
    from tqdm import tqdm
    showtqdm = True
except:
    showtqdm = False

def download_bigfile(self,url, filename, target_path, filelists=[]):
    '''
    Download Big file, use fragment download
    '''

    r = requests.get(url, stream=True)
    content_size = int(r.headers['Content-Length']) / 1024 / 1024
    with open(filename, "wb") as f:
        print "download file {}, total size: {}M".format(filename, content_size)
        if showtqdm:
            for chunk in tqdm(iterable=r.iter_content(chunk_size=1024), total=content_size, unit='M'):
                if chunk:
                    f.write(chunk)
        else:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)

以上是关于python 蟒蛇下载大型文件显示进度条的主要内容,如果未能解决你的问题,请参考以下文章

Python HTTP下载文件并显示下载进度条

太好玩了,6种Python实现实时显示进度条

太好玩了,6种Python实现实时显示进度条

Python获取下载速度并显示进度条

Python实现下载界面(带进度条,断点续传,多线程多任务下载等)

如何在python中将文件保存到excel时显示进度条?