python dowload

monkeys paw monkey at joemoney.net
Tue Feb 23 18:08:19 EST 2010


On 2/23/2010 3:17 PM, Tim Chase wrote:
> 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'?

'wb' does the trick. Thanks all!

Here is the final working code, i used an index(i)
to see how many reads took place, i have to assume there is
a default buffer size:

import urllib2
a = open('adobe.pdf', 'wb')
i = 0
for line in 
urllib2.urlopen('http://www.whirlpoolwaterheaters.com/downloads/6510413.pdf'):
     i = i + 1
     a.write(line)

print "Number of reads: %d" % i
a.close()


NEW QUESTION if y'all are still reading:

Is there an integer increment operation in Python? I tried
using i++ but had to revert to 'i = i + 1'

>
>> 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