我想以編程方式下載一些文件,但得到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()
有什麼我可以做下載較大的文件?提前致謝。
downvoter請解釋如何改善這個問題? – gwg 2014-11-21 15:02:31