Downloading Large Files -- Feedback?

Alex Martelli aleaxit at yahoo.com
Sun Feb 12 16:41:32 EST 2006


mwt <michaeltaft at gmail.com> wrote:
   ...
> The process is pretty opaque, however. This downloads and writes the
> file with no feedback whatsoever. You don't see how many bytes you've
> downloaded already, etc. Especially the "g = f.read()" step just sits
> there while downloading a large file, presenting a pregnant, blinking
> cursor.
> 
> So my question is, what is a good way to go about coding this kind of
> basic feedback? Also, since my testing has only *worked* with this

You may use urlretrieve instead of urlopen: urlretrieve accepts an
optional argument named reporthook, and calls it once in a while ("zero
or more times"...;-) with three arguments block_count (number of blocks
downloaded so far), block_size (size of each block in bytes), file_size
(total size of the file in bytes if known, otherwise -1).  The
reporthook function (or other callable) may display a progress bar or
whatever you like best.

urlretrieve saves what's downloading to a disk file (you may specify a
filename, or let it pick an appropriate temporary filename) and returns
two things, the filename where it's downloaded the data and a
mimetools.Message instance whose headers have metadata (such as content
type information).

If that doesn't fit your needs well, you may study the sources of
urllib.py in your Python's library source directory, to see exactly what
it's doing and code your own modified version.


Alex

Alex



More information about the Python-list mailing list