pythonftpprogress-barftplib

Alive-Progress bar for FTP download in Python


I am writing a piece of code to download file from FTP and display the progress of the download , and having an issue on how to pass an iterable to alive-progress package to display the progress bar.

def download(filepath,user,password,dir,filename):

    ftp = FTP('ftp.com')
    #ftp.set_debuglevel(2) 
    ftp.login(user.strip('\'\"'),password.strip('\'\"'))
    ftp.cwd(dir)
    totalSize = ftp.size(filepath)
    print(totalSize, "file blocks")

    sizeWritten=0

    def download_file(block):

        global sizeWritten
        file.write(block)
    
        sizeWritten += len(block)
        #percentComplete = (sizeWritten / totalSize)*100
        yield sizeWritten

   try:
      file = open(filename, "wb")
      ftp.retrbinary("RETR " + filepath ,download_file,blocksize=16192)
      print("Download Successful!")

   except Exception as e: 
      print (str(e))

   with alive_bar(totalSize) as bar:
       for i in download_file():
            bar()

    file.close()
    ftp.close()

The above code fails with

  File "c:\Users\automation\Automate.py", line 74, in download
for i in download_file():TypeError: download.<locals>.download_file() missing 1 required positional argument: 'block'

Is there a better way to pass an iterable to the alive-progress bar?


Solution

  • I do not know what exactly does bar() do, but I assume it progresses the progress bar. Then you have to call it from within the download_file callback.

    def download_file(block):
    
        file.write(block)
        bar()
    

    For a similar question with ProgressBar, see
    Show FTP download progress in Python (ProgressBar)