python dowload

Diez B. Roggisch deets at nospam.web.de
Wed Feb 24 16:39:44 EST 2010


Am 24.02.10 00:08, schrieb monkeys paw:
> 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'


Instead, use enumerate:

for i, line in enumerate(...):
     ...


Diez



More information about the Python-list mailing list