在 Python 中显示 FTP 下载进度(ProgressBar)

Show FTP download progress in Python (ProgressBar)(在 Python 中显示 FTP 下载进度(ProgressBar))
本文介绍了在 Python 中显示 FTP 下载进度(ProgressBar)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用以下 Python 脚本通过 FTP 下载文件.我想要的是在下载时查看进度的详细信息.为此,我使用了 ProgressBar 但它没有显示任何内容.

I am downloading files over FTP using the following Python script. What I wanted is to see the details of the progress while downloading. For that I used ProgressBar but it isn't showing anything.

这是我的代码:

import re
import os
import ftplib
import ntpath

import sys
import time

from progressbar import AnimatedMarker, Bar, BouncingBar, Counter, ETA, 
    AdaptiveETA, FileTransferSpeed, FormatLabel, Percentage, 
    ProgressBar, ReverseBar, RotatingMarker, 
    SimpleProgress, Timer, UnknownLength

ftp = ftplib.FTP("Your IP address")
ftp.login("Username", "password")
files = []

try:
    ftp.cwd("/feed_1")
    files = ftp.nlst()
    for fname in files:
        res = re.findall("2018-07-25", fname)
        if res:
            print 'Opening local file ' + ntpath.basename(fname)
            file = open(ntpath.basename(fname), 'wb')
            print 'Getting ' + ntpath.basename(fname)

            try:
                 widgets = ['Downloading: ', Percentage(), ' ',
                    Bar(marker='#',left='[',right=']'),
                    ' ', ETA(), ' ', FileTransferSpeed()]

                 pbar = ProgressBar(widgets=widgets, maxval=500)
                 pbar.start()
                 ftp.retrbinary('RETR ' + ntpath.basename(fname), file.write)

            except:
                pass

            print 'Closing file ' + ntpath.basename(fname)
            file.close() 
            print (fname)
            time.sleep(0.2)
            pbar.update()
            pbar.finish() 

        if not res:
            continue

except ftplib.error_perm , resp:
    if str(resp) == "550 No files found":
        print "No files in this directory"
        pass
    else:
        raise

请帮助了解这里的实际问题.谢谢:)

Please help in understanding what's actually wrong here. Thanks :)

推荐答案

你永远不会更新 ProgressBar.您需要做的是:

You never update the ProgressBar. What you need to do is to:

  • 实现一个函数(或类方法),您将传递给 FTP.retrbinary 作为 callback 而不是 file.write.该函数应该执行 file.write 并更新进度条.

  • Implement a function (or a class method) that you will pass to FTP.retrbinary as callback instead of file.write. The function should do file.write and also update the progress bar.

您还需要知道 ProgressBarmaxval 参数的文件/传输的大小.为此,您可以使用 FTP.size.

You also need to know size of the file/transfer for maxval argument of ProgressBar. For that you can use FTP.size.

一个简单的实现是这样的:

A trivial implementation is like:

local_path = "archive.zip"
remote_path = "/remote/path/archive.zip"

file = open(local_path, 'wb')

size = ftp.size(remote_path)

pbar = ProgressBar(widgets=widgets, maxval=size)
pbar.start()

def file_write(data):
   file.write(data) 
   global pbar
   pbar += len(data)

ftp.retrbinary("RETR " + remote_path, file_write)

现在你得到了你想要的进度条:

And now you get the progress bar you want:

Downloading:  72% [##############################           ] ETA:   0:00:00 242.1 MiB/s


其他人注意:OP 代码使用 progressbar2 库.

PyQt 实现:从另一个运行 FTP 下载的线程更新 PyQt 进度.

这篇关于在 Python 中显示 FTP 下载进度(ProgressBar)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)