Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ftplib: Show FTP upload progress

I am uploading a large file with FTP using Python 3.4.

I would like to be able to show the progress percentage while uploading the file. Here's my code:

from ftplib import FTP
import os.path

# Init
sizeWritten = 0
totalSize = os.path.getsize('test.zip')
print('Total file size : ' + str(round(totalSize / 1024 / 1024 ,1)) + ' Mb')

# Define a handle to print the percentage uploaded
def handle(block):
    sizeWritten += 1024 # this line fail because sizeWritten is not initialized.
    percentComplete = sizeWritten / totalSize
    print(str(percentComplete) + " percent complete")

# Open FTP connection
ftp = FTP('website.com')
ftp.login('user','password')

# Open the file and upload it
file = open('test.zip', 'rb')
ftp.storbinary('STOR test.zip', file, 1024, handle)

# Close the connection and the file
ftp.quit()
file.close()

How to have the number of blocks already read in the handle function?

update

After reading cmd's answer, I added this to my code:

class FtpUploadTracker:
    sizeWritten = 0
    totalSize = 0
    lastShownPercent = 0
    
    def __init__(self, totalSize):
        self.totalSize = totalSize
    
    def handle(self, block):
        self.sizeWritten += 1024
        percentComplete = round((self.sizeWritten / self.totalSize) * 100)
        
        if (self.lastShownPercent != percentComplete):
            self.lastShownPercent = percentComplete
            print(str(percentComplete) + " percent complete")

And I call the FTP upload like this :

uploadTracker = FtpUploadTracker(int(totalSize))
ftp.storbinary('STOR test.zip', file, 1024, uploadTracker.handle)
like image 583
Gab Avatar asked Feb 24 '14 20:02

Gab


1 Answers

There are three non-hacky ways I can think of. All of then shift the "ownwership" of the variable:

  1. have the value passed in and return the result (basically means its stored in the caller)
  2. have the value be global, and initialize it to 0 and the top of your file. (read up on the global keyword)
  3. have this function as a member function of a class to handle upload tracking. Then make sizeWritten a instance variable of that class.
like image 175
cmd Avatar answered Oct 11 '22 02:10

cmd