views:

115

answers:

1

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
+7  A: 

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.

Alex Martelli
Thanks. Works a treat now. I'd never noticed a difference between "w" and "wb" working with files locally, even with images.
John Gann