When I run the following code, it only seems to be downloading the first little bit of the file and then exiting. Occassionally, I will get a 10054 error, but usually it just exits without getting the whole file. My internet connection is crappy wireless, and I often get broken downloads on larger files in firefox, but my browser has no problem getting a 200k image file. I’m new to python, and programming in general, so I’m wondering what nuance I’m missing.
import urllib2
xkcdpic=urllib2.urlopen("http://imgs.xkcd.com/comics/literally.png")
xkcdpicfile=open("C:\\Documents and Settings\\John Gann\\Desktop\\xkcd.png","w")
while 1:
chunk=xkcdpic.read(4028)
if chunk:
print chunk
xkcdpicfile.write(chunk)
else:
break
To write a binary file on Windows you need to explicitly open it as binary, i.e.:
note the extra
bin the options:"wb", not just"w"!I would also recommend losing the
print chunkwhich may send arbitrary binary sequences to the console and possibly caused undesired side effects. If you’re keen to see the hex bytes whizz by meaninglessly, maybeprint repr(chunk), if you insist. But I’d find something more meaningful to show, e.g.len(chunk)and maybe the total number of bytes so far.