zoukankan      html  css  js  c++  java
  • Python3实现文件下载,显示下载进度

    Python3实现文件下载,显示下载进度

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    #   实现文件下载,显示下载进度
    
    import os
    import requests
    
    
    def download_file(url, save_path=""):
        if save_path == "":
            save_path = url.split(os.path.sep)[-1]
        with requests.get(url, stream=True) as fget:
            # 此时只有响应头被下载
            file_size = int(fget.headers["Content-Length"])
            print('-' * 32)
            print(f"Name: {save_path}")
            print(f"Size: {file_size/(1000**2)}Mb")
            print(f"Link: {url}")
            print('-' * 32)
            chunk_size = 512
            file_done = 0
            with open(save_path, "wb") as fw:
                for chunk in fget.iter_content(chunk_size):
                    fw.write(chunk)
                    file_done = file_done + chunk_size
                    percent = file_done / file_size
                    if file_done <= file_size:
                        print(f"Download: {percent:.2%}", end='
    ')
                    else:
                        print("Download: 100%  ")
    
    
    if __name__ == "__main__":
        download_file("https://www.baidu.com/img/flexible/logo/pc/result.png")
    
  • 相关阅读:
    并发量,tps,qps
    MYSQL安装和配置
    python 生成随机数的几种方法
    python 判断是字母的多种方法
    python实战,
    linux工作常用命令
    apache http server安装
    .py与.pyc文件区别
    上传本地文件到linux
    ms
  • 原文地址:https://www.cnblogs.com/srczhang/p/13719374.html
Copyright © 2011-2022 走看看