西北乱跑娃 --- python分段下载文件io流

一、下载文件代码

这里使用requests下载文件,设置stream=True参数的意义在于先不拿取响应数据,只取header响应头数据中的文件大小信息。文件写入模式为ab+,意思为将二进制流的形式写入到文件,且不需要关注光标位置,自动累加到文件最后,且不覆盖之前的信息。

import time
import random
import requests

res = requests.get('https://pypi.doubanio.com/packages/79/87/8bb36bd4ebae147612c73d1bdc1385db7beebb9eb141c4bfefb33f52c87c/pandas-1.1.5-cp36-cp36m-win_amd64.whl#sha256=19a2148a1d02791352e9fa637899a78e371a3516ac6da5c4edc718f60cbae648', stream=True)
size = int(res.headers['content-length']) // 1024


def percent(p, b):
    print(f'\r下载中:{"▒" * p}{b}%', end='')


def download():
    b = []
    with open('pandas-1.1.5-cp36-cp36m-win_amd64.whl', 'ab+') as f:
        for i, chunk in enumerate(res.iter_content(chunk_size=1024)):
            if chunk:
                b.append(i)
                bf = int(len(b)/size*100)
                per = int(bf/4)
                percent(per, bf)
                f.write(chunk)


            else:
                print('下载失败!')
                break

download()

二、下载成功打印信息


下载中:▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒100%
Process finished with exit code 0

三、使用安装测试文件是否正确

C:\Users\Administrator\Desktop\program>pip install pandas-1.1.5-cp36-cp36m-win_amd64.whl
Looking in indexes: https://pypi.douban.com/simple/
Processing c:\users\administrator\desktop\program\pandas-1.1.5-cp36-cp36m-win_amd64.whl
Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from pandas==1.1.5) (2.8.1)
Requirement already satisfied: numpy>=1.15.4 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from pandas==1.1.5) (1.19.5)
Requirement already satisfied: pytz>=2017.2 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from pandas==1.1.5) (2021.1)
Requirement already satisfied: six>=1.5 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from python-dateutil>=2.7.3->pandas==1.1.5) (1.16.0)
Installing collected packages: pandas
Successfully installed pandas-1.1.5

nice, o jb k

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西北乱跑娃

万水千山总是情,犒赏一下行不行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值