【问题标题】:Why does my program stop downloading very large files before finishing the full download?为什么我的程序在完成完整下载之前停止下载非常大的文件?
【发布时间】:2018-10-09 14:28:19
【问题描述】:

我正在使用这种方法从一些 http 链接下载文件。这些文件的大小通常超过 20 GB,程序似乎可以正常工作,但在完成之前会转到下一个功能。

import urllib2
import os
def saveFile(url):
    print url
    file_name = url.split('/')[-1]
    #check if the file was already downloaded previously to save time
    if os.path.isfile(file_name):
        print("File already exists, skipping download...")
        return file_name
    u = urllib2.urlopen(url)
    f = open(file_name, 'wb')
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading: %s Bytes: %s" % (file_name, file_size)

    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print status,    
    f.close()
    return file_name

【问题讨论】:

    标签: python download urllib2


    【解决方案1】:

    您是否检查过流程管理器?可能内存已被占用超过限制,出现OutOfMemoryError

    【讨论】:

    • 我相信我发现问题是由于文件很大,每 30 分钟左右会发生 2 秒超时。我正在尝试设置 20 秒的超时计时器,看看是否有帮助。
    • 这听起来很合理。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2012-12-31
    相关资源
    最近更新 更多