2014-11-21 127 views
0

我想以編程方式下載一些文件,但得到MemoryError例外較大的。例如,當我嘗試下載small file時,代碼沒有問題,但是當我嘗試下載larger file時,我收到了MemoryError如何在Python中沒有MemoryError的情況下下載大文件?

這裏是我的代碼:

def __download_gpl_file(accession): 
    try: 
     bin_string = __get_response(accession) 
     if bin_string is None: 
      return False 
     string = __unzip(bin_string) 
    except MemoryError: 
     print 'Out of memory for: ' + accession 
     return False 

    if string: 
     filename = DOWNLOADED + accession + '.txt' 
     with open(filename, 'w+') as f: 
      f.write(string) 
     return True 
    return False 


def __get_response(attempts=5): 
    url = __construct_gpl_url(accession) # Not shown 
    response = None 
    while attempts > 0: 
     try: 
      response = urllib2.urlopen(url) 
      if response and response.getcode() < 201: 
       break 
      else: 
       attempts -= 1 
     except urllib2.URLError: 
      print 'URLError with: ' + url 
    return response.read() 


def __unzip(bin_string): 
    f = StringIO(bin_string) 
    decompressed = gzip.GzipFile(fileobj=f) 
    return decompressed.read() 

有什麼我可以做下載較大的文件?提前致謝。

+0

downvoter請解釋如何改善這個問題? – gwg 2014-11-21 15:02:31

回答

3

而不是一次寫整個文件,您可以通過在線寫行:

file = urllib2.urlopen('url') 
with open('filename','w') as f: 
    for x in file: 
     f.write(x) 
,如果你想讓它更快速

file = urllib2.urlopen('url') 
with open('filename','w') as f: 
    while True: 
     tmp = file.read(1024) 
     if not tmp: 
      break 
     f.write(tmp) 
2

我沒有足夠的積分評論關於哈克霍迪奇的回答,所以我的回答只是他的第一個例子,但稍作修改。

file = urllib2.urlopen('url') 
with open('filename','w') as f: 
    for x in file: 
     f.write(x) 

我認爲他寫了f.write(f)意外。

+0

謝謝。我已經編輯了他的答案。 – gwg 2017-06-14 15:27:44

相關問題