Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I download a whole image file with urllib2.urlopen()

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
like image 345
John Gann Avatar asked Apr 11 '10 00:04

John Gann


1 Answers

To write a binary file on Windows you need to explicitly open it as binary, i.e.:

xkcdpicfile=open("C:\\Documents and Settings\\John Gann\\Desktop\\xkcd.png",
                 "wb")

note the extra b in the options: "wb", not just "w"!

I would also recommend losing the print chunk which 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, maybe print 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.

like image 179
Alex Martelli Avatar answered Dec 01 '22 08:12

Alex Martelli