ftplib progress code example
Example 1: ftplib progress
>>> import ftplib
>>> import progressbar
>>>
>>> ftp = ftplib.FTP('ftp.myserver.com', 'user', 'passwd')
>>> filesize = ftp.size('path/to/remotefile.zip')
>>> progress = progressbar.AnimatedProgressBar(end=filesize, width=50)
>>>
>>> with open('localfile.zip', 'w') as f:
>>> def callback(chunk):
>>> f.write(chunk)
>>> progress + len(chunk)
>>>
>>> # Visual feedback of the progress!
>>> progress.show_progress()
>>>
>>> ftp.retrbinary('RETR path/to/remotefile.zip', callback)
Example 2: ftplib progress
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()