python - memoryError: out of memory -


i wrote program download app,movie , other.but when run give me error:

traceback (most recent call last):   file "tidopy.py", line 27, in <module> data=urllib2.urlopen(addresslist[x]).read()   file "c:\python27\lib\socket.py", line 362, in read   buf.write(data) memoryerror: out of memory 

i think error code:

data=urllib2.urlopen(addresslist[x]).read() file=open(namelist[x],'wb') file.write(data) 

data variable downloads data of movies , other

file makes file

and file.write(data) puts data in file

tidopy program name..

how can fix it?

help.

it seems you're downloading files url , writing file, during expect data hold entire file, happens in computer's memory (ram of pagefile) runs out, when files big (like movies).

the solution write each chunk of file downloaded, , not load entire thing memory , write it. have small code that, if want use it:

import requests # choice of comfort me def download(url_address, filename):     response = requests.get(url_address, stream=true)     response.raise_for_status()     open(filename, "wb") f:         total_length = response.headers.get('content-length')         if total_length none:             f.write(response.content)         else:             total_length = int(total_length)             data in response.iter_content(chunk_size = total_length / 100):                 f.write(data) 

if notice, i've set chunk_size = total_length / 100 means every time download in data reaches 1% it's writing file, , it's replaced next 1% of data comes in, takes 1% of data stored in memory. if files downloading big handle 1% in memory, should replace chunk_size fixed number of bytes, maybe 1,000,000 (which 1 mb)


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -