Downloading Large Files -- Feedback?

Paul Rubin http
Sun Feb 12 15:12:48 EST 2006


"mwt" <michaeltaft at gmail.com> writes:
> f =  urllib.urlopen("http://www.python.org/blah/blah.zip")
> g = f.read()   # ...

> 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
> code, I'm curious if it will throw a visibile error if something goes
> wrong with the download.

One obvious type of failure is running out of memory if the file is
too large.  Python can be fairly hosed (VM thrashing etc.) by the time
that happens.  Normally you shouldn't read a potentially big file of
unknown size all in one gulp like that.  You'd instead say something
like

   while True:
      block = f.read(4096)   # read a 4k block from the file
      if len(block) == 0:
         break               # end of file
      # do something with the block

Your "do something with..." could involve updating a status display 
or something, saying how much has been read so far.



More information about the Python-list mailing list