python - 下载文件时如何在每个块之后提交/写入数据,以便它不会使用所有内存?
问题描述
当我下载文件时,我可以看到内存正在被用于存储。有没有办法在下载每块 1GB 数据后提交数据?
submission_path = r'D:\Users\Jonathan\Desktop\Reddit Data\ETL-Python\\'
for download_file in clean_matching_list_of_href:
save_file_w_submission_path = submission_path + download_file
constructured_url = url_to_download + download_file
request = urllib.request.Request(constructured_url)
response = urllib.request.urlopen(request)
data_content = response.read()
shutil.copyfileobj(save_file_w_submission_path,data_content,length = 10000)
更新: 所以我改用了 write,如下所示:
for download_file in matching_list_of_href:
filename = download_file[download_file.rfind("/")+1:]
save_file_w_submission_path = path_to_save_document + filename
request = urllib.request.Request(download_file)
response = urllib.request.urlopen(request)
data_content = response.read()
with open(save_file_w_submission_path, 'wb') as wf:
wf.write(data_content)
print(save_file_w_submission_path)
解决方案
推荐阅读
- javascript - 当用户在提示中输入他的名字时,我的 JavaScript If-Statement 代码没有给出正确的结果
- jenkins - 想用 jenkins 参数参数化 ansible playbook 中的值
- google-play - 为什么我在 Play 商店中有效的最新 Google App 版本在第一次更新时不适用于现有用户
- javascript - 如何让 DataTables 搜索与 AJAX 结果一起正常工作?
- javascript - 为什么我的 API 调用函数返回一个未定义的对象?
- python - 如何在没有 python 控制台的情况下运行 tkinter 应用程序
- python - 如何获取与用户模型一对一相关的对象
- google-bigquery - Google Data Studio - BigQuery 数据源列表,如何跟踪用作数据源的 bigquery 表
- c - 如何清除已在 C 编程中使用的字符串的内容?
- c# - 我在 while 循环中的计数出现错误,C# Visual Studio