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.
Here's my code:
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 :)
You never update the
ProgressBar
. What you need to do is to:Implement a function (or a class method) that you will pass to
FTP.retrbinary
ascallback
instead offile.write
. The function should dofile.write
and also update the progress bar.You also need to know size of the file/transfer for
maxval
argument ofProgressBar
. For that you can useFTP.size
.A trivial implementation is like:
And now you get the progress bar you want:
Note for others: The OP code uses
progressbar2
library.PyQt implementation: Update PyQt progress from another thread running FTP download.