I would like to see progress bar while downloading the file from FTP server. My code looks like this but I get a lot of errors and I don't know why It's happening: NOTE: this is just a "test" code and it is supposed to only print the completed percentage of downloaded file.
def download_file():
ftp = FTP('exampledomain.com')
ftp.login(user='user', passwd='pass')
ftp.cwd('/some_dir/')
filename = 'file.txt'
ftp.sendcmd("TYPE i")
totalsize = ftp.size(filename)
totalsize = round(totalsize / 1024 / 1024, 1)
ftp.sendcmd("TYPE A")
dltracker = FtpDownloadTracker(int(totalsize))
with open(filename, 'wb') as localfile:
ftp.retrbinary('RETR ' + filename, localfile.write, 1024, dltracker.handle)
ftp.quit()
localfile.close()
class FtpDownloadTracker():
sizeWritten = 0
totalSize = 0
lastPercent = 0
def __init__(self, totalsize):
self.totalSize = totalsize
def handle(self, block):
self.sizeWritten += 1024
percentComplete = round((self.sizeWritten / self.totalSize) * 100)
if self.lastPercent != percentComplete:
self.lastPercent = percentComplete
print(str(percentComplete) + " percent complete")
and I get these errors:
Traceback (most recent call last):
File "D:/PythonProjects/PythonFTP/ftpdl.py", line 148, in run
ftp.retrbinary('RETR ' + filename, localfile.write, 1024, dltracker.handle)
File "C:\Python\lib\ftplib.py", line 442, in retrbinary
with self.transfercmd(cmd, rest) as conn:
File "C:\Python\lib\ftplib.py", line 399, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python\lib\ftplib.py", line 364, in ntransfercmd
self.sendcmd("REST %s" % rest)
File "C:\Python\lib\ftplib.py", line 273, in sendcmd
return self.getresp()
File "C:\Python\lib\ftplib.py", line 246, in getresp
raise error_perm(resp)
ftplib.error_perm: 554-REST needs a numeric parameter
554 Restart offset reset to 0
What am I doing wrong? I can notice there is something called "raise error_perm(resp)" Is it something with server permissions or whatever it is?