0

I have been working all day trying to figure out how to use the python ftplib module to download folders, subfolders, and files from an ftp server but I could only come up with this.

from ftplib import FTP
import sys, ftplib

sys.tracebacklimit = 0 # Does not display traceback errors
sys.stderr = "/dev/null" # Does not display Attribute errors

Host = "ftp.debian.org"
Port = 21
Username = ""
Password = ""

def MainClass():
    global ftp
    global con
    Host
    Port
    ftp = FTP()
    con = ftp.connect(Host, Port) # Connects to the host with the specified port

def grabfile():
    source = "/debian/"
    filename = "README.html"
    ftp.cwd(source)
    localfile = open(filename, 'wb')
    ftp.retrbinary('RETR ' + filename, localfile.write)
    ftp.quit()
    localfile.close()

try:
    MainClass()
except Exception:
    print "Not Connected"
    print "Check the address", Host + ":" + str(Port)
else:
    print "Connected"

if ftplib.error_perm and not Username == "" and Password == "":
    print "Please check your credentials\n", Username, "\n", Password

credentials = ftp.login(Username, Password)
grabfile()

This python script will download a README.html file from ftp.debian.org but, I would like to be able to download whole folders with files and subfolders in them and I cannot seem to figure that out. I have searched around for different python scripts using this module but I cannot seem to find any that do what I want.

Any suggestions or help would be greatly appreciated.

Note: I would still like to use python for this job but it could be a different module such as ftputil or any other one out there.

Thanks in advance, Alex

Alex Lowe
  • 783
  • 3
  • 20
  • 43

1 Answers1

1

The short solution: You could possibly just run: "wget -r ftp://username:password@ftp.debian.org/debian/*" to get all the files under the debian directory. Then you can process the files in python.

The long solution: You can go over every directory listing by using ftplib, getting a directory listing parsing it and then getting every file and recursing into directories. If you search the web you'd find previous posts on stackoverlow which solve this issue

Community
  • 1
  • 1
borisp
  • 180
  • 6
  • That is so funny that you bring up that post because that is the code that did not make sense to me. – Alex Lowe Oct 10 '15 at 05:44
  • could you please explain what this line of code is doing in that post?`os.mkdir(destination[0:len(destination)-1]+path)` – Alex Lowe Oct 10 '15 at 05:46
  • `path` - path to the new directory being copied from the server. `directory` - the destination directory where are files will be copied to. So this line creates the directory into which the files in `path` will be copied to – borisp Oct 10 '15 at 05:51