python dowload

John Bokma john at castleamber.com
Tue Feb 23 21:09:50 CET 2010


monkeys paw <monkey at joemoney.net> writes:

> 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')
> for line in urllib2.urlopen(url):
>     a.write(line)

pdf is /not/ text. You're processing it like it's a text file (and
storing it like it's text, which on Windows is most likely a no no).

import urllib2

url = 'http://www.whirlpoolwaterheaters.com/downloads/6510413.pdf'
response = urllib2.urlopen(url)
fh = open('adobe.pdf', 'wb')
fh.write(response.read())
fh.close()
response.close()

-- 
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development



More information about the Python-list mailing list