【问题标题】:Python progressbar for FTP downloads not working用于 FTP 下载的 Python 进度条不起作用
【发布时间】:2019-04-11 02:10:09
【问题描述】:

我正在尝试使用进度条显示 FTP 文件下载进度 (ftplib),但进度未正确更新。速度开始很高,然后逐渐降低(下降到字节)。几秒钟后下载完成,而进度条仍为 0%。看来我没有正确更新进度,不知道如何更正。

我尝试使用pbar += len(data)Show FTP download progress in Python (ProgressBar) 找到的解决方案,但这给了我以下错误:

Traceback (most recent call last):                                                             ] ETA:  --:--:--   0.00  B/s
  File "ftp.py", line 38, in <module>
    ftp.retrbinary('RETR ' + file, file_write)
  File "/usr/lib/python3.5/ftplib.py", line 446, in retrbinary
    callback(data)
  File "ftp.py", line 29, in file_write
    pbar += len(data)
TypeError: unsupported operand type(s) for +=: 'ProgressBar' and 'int'

所以我通过在我的file_write() 函数中添加pbar.update(len(data)) 对其进行了调整,并让它在没有错误的情况下工作,但正如我所说的速度完全不正确,不断下降(直到它可能达到 0)然后突然完成.

这是我的整个脚本:

from ftplib import FTP_TLS
import time

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

ftp_host = 'domain.com'
ftp_port = 21
ftp_user = 'user'
ftp_pass = 'pass'

ftp = FTP_TLS()

ftp.connect(ftp_host, ftp_port)
ftp.login(ftp_user, ftp_pass)
ftp.cwd('/videos')

files = ftp.nlst()

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

def file_write(data):
    localfile.write(data)
    global pbar
    pbar.update(len(data))
    #pbar += len(data)

for file in files:
    size = ftp.size(file)
    pbar = ProgressBar(widgets = widgets, maxval = size)
    pbar.start()

    localfile = open('/local/videos/' + file, 'wb')

    ftp.retrbinary('RETR ' + file, file_write)

    pbar.finish() 
    localfile.close()

ftp.quit()

非常感谢任何帮助以使此代码正常工作。

更新:

我做了以下添加/更改并获得了正确的速度/进度条移动:

i = 0
def file_write(data):
    localfile.write(data)
    global pbar, i
    pbar.update(i * 1024 * 10)
    i+=1
    #pbar += len(data)

但就在它即将完成时,我收到了这个错误:

Traceback (most recent call last):################################################## ] ETA:  0:00:00  45.62 MB/s
  File "ftp.py", line 42, in <module>
    ftp.retrbinary('RETR ' + file, file_write)
  File "/usr/lib/python3.5/ftplib.py", line 446, in retrbinary
    callback(data)
  File "ftp.py", line 30, in file_write
    pbar.update(o * 1024 * 10)
  File "/usr/local/lib/python3.5/dist-packages/progressbar/progressbar.py", line 250, in update
    raise ValueError('Value out of range')
ValueError: Value out of range

我正在使用进度条 2.5(最新)和 Python 3.5。

【问题讨论】:

  • 使用pbar += len(data) 对我来说工作得很好——不支持的操作数的错误是x = pbar + len(data)——可能是你的一些测试,这与你发布的实际代码无关。 --- 我看不出你的 pbar.update(i * 1024 * 10) 有什么意义 --- 很明显为什么你得到 ValueError 的代码。但是代码(数字)是完全任意的。它可以根据您的特定下载速度随机工作,但通常无法显示正确的值。
  • 我复制了错误的错误,但是当我使用pbar += len(data) 时,我得到TypeError: unsupported operand type(s) for +=: 'ProgressBar' and 'int' 我正在使用progressbar 2.5(最新)和Python 3.5

标签: python ftp progress-bar ftplib


【解决方案1】:

代码实际上是progressbar2 library

它的ProgressBar class 实现了__iadd__

【讨论】:

  • 谢谢!!一旦将进度条替换为进度条2,一切都会按预期工作。旧的进度条应该从 python 中删除,因为它似乎没有维护并且缺少文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2016-01-03
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多