您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用urllib2将大型二进制文件流式传输到文件

使用urllib2将大型二进制文件流式传输到文件

没有理由一行一行地工作(小块并且需要Python来为您找到行尾!-),只需将其分成更大的块即可,例如:

# from urllib2 import urlopen # Python 2
from urllib.request import urlopen # Python 3

response = urlopen(url)
CHUNK = 16 * 1024
with open(file, 'wb') as f:
    while True:
        chunk = response.read(CHUNK)
        if not chunk:
            break
        f.write(chunk)

尝试各种CHUNK尺寸,找到适合您需求的“最佳位置”。

其他 2022/1/1 18:29:15 有501人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶