python dowload

Tim Chase python.list at tim.thechases.com
Tue Feb 23 21:17:11 CET 2010


monkeys paw wrote:
> I used the following code to download a PDF file, but the
> file was invalid after running the code, is there problem
> with the write operation?
> 
> import urllib2
> url = 'http://www.whirlpoolwaterheaters.com/downloads/6510413.pdf'
> a = open('adobe.pdf', 'w')

Sure you don't need this to be 'wb' instead of 'w'?

> for line in urllib2.urlopen(url):
>      a.write(line)

I also don't know if this "for line...a.write(line)" loop is 
doing newline translation.  If it's a binary file, you should use 
.read() (perhaps with a modest-sized block-size, writing it in a 
loop if the file can end up being large.)

-tkc





More information about the Python-list mailing list