Download all files from FTP in Python
You can use this Python script to download / clone entire FTP directory recursively from remote FTP Host. Let’s say you would like to download www-data directory and all sub directories inside this one from ftp.test.com server.
#!/usr/bin/python import sys import ftplib import os import time server = "FTPHOST" user = "anonymous" password = "anonymous" source = "/Folder/SourceFolder/" destination = "/home/user/downloads/" interval = 0.05 ftp = ftplib.FTP(server) ftp.login(user, password) def downloadFiles(path, destination): try: ftp.cwd(path) os.chdir(destination) mkdir_p(destination[0:len(destination)-1] + path) print "Created: " + destination[0:len(destination)-1] + path except OSError: pass except ftplib.error_perm: print "Error: could not change to " + path sys.exit("Ending Application") filelist=ftp.nlst() for file in filelist: time.sleep(interval) try: ftp.cwd(path + file + "/") downloadFiles(path + file + "/", destination) except ftplib.error_perm: os.chdir(destination[0:len(destination)-1] + path) try: ftp.retrbinary("RETR " + file, open(os.path.join(destination + path, file),"wb").write) print "Downloaded: " + file except: print "Error: File could not be downloaded " + file return def mkdir_p(path): try: os.makedirs(path) except OSError as exc: if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: raise downloadFiles(source, destination)
Comments
Getting socket error while connecting (noaddrfound) and I am not able to understand what is "path" here in this code?
I was receiving the same errors. Make sure the original string for server doesn't start with 'ftp://' and ends when the host address ends, not the path. Then you enter the path that comes after.com/.org/.gov into the source variable. For example: URI: ftp://testftp.org/OneFolder/SecondFolder/ server = "testftp.org" source = "/OneFolder/SecondFolder/" Hope this helps Patrick
I was receiving the same errors. Make sure the original string for server doesn't start with 'ftp://' and ends when the host address ends, not the path. Then you enter the path that comes after.com/.org/.gov into the source variable. For example: URI: ftp://testftp.org/OneFolder/SecondFolder/ server = "testftp.org" source = "/OneFolder/SecondFolder/" Hope this helps Patrick
I want copy all files from local to FTP in Python.can you modify it i am beginner so i can't
This script works like a charm but I would like to download the whole root, not just a specified directory. The following setting is not working: source = "/" Can somebody please tell me how to set the right source variable?
Hi, This script works like a charm but I would like to download the whole root, not just a specified directory. The following setting is not working: source = "/" Can somebody please tell me how to set the right source variable?
Great script! Has anyone tried updating this script to only download files from particular years within a directory that has files organized in year subdirectories?