Python 下载超大文件
2024-10-19 14:30:40
使用python下载超大文件, 直接全部下载, 文件过大, 可能会造成内存不足, 这时候要使用requests 的 stream模式,
主要代码如下
iter_content:一块一块的遍历要下载的内容
iter_lines:一行一行的遍历要下载的内容
def download_file(url, file_pname, chunk_size=1024*4):
"""
url: file url
file_pname: file save path
chunk_size: chunk size
"""
# 第一种
response_data_file = requests.get(url, stream=True)
with open(file_pname, 'wb') as f:
for chunk in response_data_file.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk) # 第二种
with requests.get(url, stream=True) as req:
with open(file_pname, 'wb') as f:
for chunk in req.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
如有错误欢迎指出
最新文章
- OneDrive无法正常登录
- c++ 类模版、成员函数模版、函数模版 用法
- Web Service实例——天气预报
- jquery.tablesorter.js 学习笔记
- 使用apache反向代理tomacat
- RESTClient
- freemarker写select组件报错总结(三)
- 线程同步Volatile与Synchronized(一)
- sqlserver修改计算机名称。
- C# 之 HttpRequest 类
- PostMessage 解析
- aspose.cells 插入图片
- html 统一资源定位器(url)和url编码
- [PHP] 05 - Cookie &; Session
- HTML5 简单归纳 -- 前端知识 (一)
- 缩点tarjan
- MongoDB存储基础教程
- Python常用time处理
- android activity 启动模式
- 子类化GetOpenFileName/GetSaveFileName, 以及钩子函数OFNHookProc的使用的简要说明