Question

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)
Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top