python dowload

monkeys paw monkey at
Wed Feb 24 00:08:19 CET 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 = ''
>> 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 
     i = i + 1

print "Number of reads: %d" % i

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