我正在尝试使用进度条显示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'
所以我通过在pbar.update(len(data))
函数中添加file_write()
来对其进行了调整,并使其正常工作而没有错误,但是正如我所说的,速度是完全不正确的,一直在下降(直到它达到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
我正在使用Progressbar 2.5(最新版)和Python 3.5。