2

I've tried the script below:

import os
from ftplib import FTP

ftp = FTP("ftpsite","myuser", "mypass")
ftp.login()
ftp.retrlines("LIST")

ftp.cwd("folderOne")
ftp.cwd("subFolder")

listing = []
ftp.retrlines("LIST", listing.append)
words = listing[0].split(None, 8)
filename = words[-1].lstrip()

#download the file
local_filename = os.path.join(r"C:\example", file)
lf = open(local_filename, "wb")
ftp.retrbinary("RETR " + filename, lf.write, 8*1024)
lf.close()

But everytime I run the script, it says:

Traceback (most recent call last):
  File "C:\User\Desktop\sample\ex.py", line 4, in <module>
    ftp = FTP("ftpsite", "myuser", "mypass")
  File "C:\Python27\lib\ftplib.py", line 119, in __init__
    self.login(user, passwd, acct)
  File "C:\Python27\lib\ftplib.py", line 387, in login
    resp = self.sendcmd('USER ' + user)
  File "C:\Python27\lib\ftplib.py", line 244, in sendcmd
    return self.getresp()
  File "C:\Python27\lib\ftplib.py", line 219, in getresp
    raise error_perm, resp
error_perm: 530 Permission denied.

I don't know what 530 Permission Denied means.Can anyone tell me what does that means?

falsetru
  • 357,413
  • 63
  • 732
  • 636
lovelyvm
  • 127
  • 2
  • 16

1 Answers1

1
  • It seems like the ftp server allows anonymous access; You don't need pass username, password.
  • FTP constructor accept hostname(or IP), not URL.

import sys
import os
from ftplib import FTP

ftp = FTP("ftpsite.com")
ftp.login()
ftp.cwd("/ftp/site/directory/")

listing = []
ftp.retrlines("LIST", listing.append)
words = listing[0].split(None, 8)
filesize = int(words[4])
filename = words[-1].lstrip()

class VerboseWriter:
    def __init__(self, lf, filesize):
        self.progress = 0
        self.lf = lf
        self.filesize = filesize
    def write(self, data):
        self.lf.write(data)
        self.progress += len(data)
        sys.stdout.write('\r{}/{} ({:.1%})'.format(self.progress, self.filesize, float(self.progress)/self.filesize))
        sys.stdout.flush()

#download the file
with open(os.path.join(r"c:\example", filename), 'wb') as f:
    ftp.retrbinary("RETR " + filename, VerboseWriter(lf, filesize).write, 8*1024)
print
ftp.quit()
lovelyvm
  • 127
  • 2
  • 16
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @precy, In my code there's no `file`, but `filename`. Change `file` with `filename` and try again. – falsetru Jul 22 '13 at 06:30
  • thanks it works! it already download the file. Would you mind if I ask another question? – lovelyvm Jul 22 '13 at 06:57
  • What should I add in my script to indicate that the file is downloading? Because in the python shell it only appears blank but then when you check on your folder the file was already downloaded. – lovelyvm Jul 22 '13 at 07:40
  • @precy, I updated the code to print dots while downloading the file. (`verbose_write` instead of `lf.write`) – falsetru Jul 22 '13 at 07:46
  • Thanks! But there were too many dots appearing. What about a **downloading file...** only appears and a **percentage bar**. Is it possible? – lovelyvm Jul 22 '13 at 08:24
  • @precy, Use the Carriage Return (`\r`). – falsetru Jul 22 '13 at 08:32
  • @precy, Search the question, if not found, post another question. it's whole different kind of question. – falsetru Jul 23 '13 at 02:34