views:

65

answers:

1

I am trying to write a file downloader that has a GUI and displays the progress of the file being downloaded. I would like it to either display a text percentage, a progress bar or both. I am sure this can be done in Python, but I'm just not sure how.

I am using Python 2.6 on MS Windows XP.

+2  A: 

The easiest progress bar dialog would probably be with EasyDialogs for Windows (follows the same api as the EasyDialogs module that is included with the mac version of python)

For determining the progress of the download, use urllib.urlretrieve() with a "reporthook".

Something like this:

import sys
from EasyDialogs import ProgressBar
from urllib import urlretrieve

def download(url, filename):
    bar = ProgressBar(title='Downloading...', label=url)

    def report(block_count, block_size, total_size):
        if block_count == 0:
            bar.set(0, total_size)
        bar.inc(block_size)

    urlretrieve(url, filename, reporthook=report)

if __name__ == '__main__':
    url = sys.argv[1]
    filename = sys.argv[2]
    download(url, filename)

There are of course other libraries available for richer GUI interfaces (but they are larger or more difficult if this is all you need). The same goes for the downloads: there are probably faster things than urllib, but this one is easy and included in the stdlib.

Steven
Thank you for this code snippet. It is proving very useful. However, I do have a couple other questions. 1). Where does it save the file? I assume in the active directory? 2). How would I incorporate this into a Tkinter window?
Zachary Brown
I made a couple modifications and this code worked out perfectly! Thanks!!
Zachary Brown
Ok, I am using your code, but the progress bar dialog doesn't close after it is completed. So, I put del bar after then urlretrieve line, but I keep getting the following error: "can not delete variable 'bar' referenced in nested scopeI have tried moving it around, but can't seem to find a solution.
Zachary Brown
As for saving the files: current working directory if you only provide a filename, otherwise the path that you provide. Just like open() would do for example. I don't know much about Tkinter, so I can't help you there. The progressbar should disappear once dereferenced. In this case when the download() function returns. You shouldn't need to use del. Could you post your code? It works for me (although if I add the del bar, I get the same error, this is because bar is referenced within the report function as well). You might also try bar = None instead of del bar.
Steven