Skip to content Skip to sidebar Skip to footer

Downloading A File In Python

import urllib2, sys if len(sys.argv) !=3: print 'Usage: download.py ' sys.exit(1) site = urllib2.urlopen(sys.argv[1]) meta

Solution 1:

using urllib.urlretrieve


    import urllib, sys

    def progress_callback(blocks, block_size, total_size):
        #blocks->data downloaded so far (first argument of your callback)
        #block_size -> size of each block
        #total-size -> size of the file
        #implement code to calculate the percentage downloaded e.g
        print "downloaded %f%%" % blocks/float(total_size)

    if len(sys.argv) !=3:
        print "Usage: download.py  "
        sys.exit(1)

    site = urllib.urlopen(sys.argv[1])
    (file, headers) = urllib.urlretrieve(site, sys.argv[2], progress_callback)
    print headers


Solution 2:

To display the filename: print f.name

To see all the cool things you can do with the file: dir(f)

I'm not sure I know what you mean when you say:

how to display how long it has before the file is finished downloading

If you want to display the time it took for the download, then you might want to take a look at the timeit module.

I this is not what you are looking for, then please update the question, so I can try to give you a better answer


Post a Comment for "Downloading A File In Python"