Getting the bytes or percent uploaded/downloaded through FTP?

Benji York benji at benjiyork.com
Mon Sep 5 11:10:13 EDT 2005


Nainto at gmail.com wrote:
> Hi, I'm just wondering if there is any way to get the number of bytes,
> or the percentage, that have been uploaded/downloaded when
> uploading/downloading a file throught ftp in Python. I have searched
> Google many times but could find nothing.

If you're using urllib you can do something like this:

def reporter(block, block_size, total_size):
     left = total_size - block * block_size
     sys.stderr.write('\r')
     sys.stderr.write('Downloading %s: ' % file_name)
     if left > 0: # the estimate is a bit rough, so we fake it a little
         sys.stderr.write('%sK left.' % (left/1024))
     else:
         sys.stderr.write('done.')

     # it's possible that this line is shorter than the previous,
     # so we need to "erase" any leftovers
     sys.stderr.write(' '*10)
     sys.stderr.write('\b'*10)

import urllib
urllib.urlretrieve(url, destination, reporter)
sys.stderr.write('\n')
--
Benji York




More information about the Python-list mailing list