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)
There are three non-hacky ways I can think of. All of then shift the "ownwership" of the variable:
global
keyword) sizeWritten
a instance variable of that class. 上一篇: 函数是任意许多其他函数的总和