0

In the question here we learn how to get files using FTP in python. The answer is what I have in my code, and also as follows:

import shutil
import urllib.request as request
from contextlib import closing

with closing(request.urlopen('ftp://server/path/to/file')) as r:
    with open('file', 'wb') as f:
        shutil.copyfileobj(r, f)

How do I go about now saving these files locally? This path/to/file has a bunch of directories and files within nested directories. I'd ideally like to download the root directory and have all the files and folders within.

As I am new to both Python and FTP, I am not 100% sure that my question is as clear as possible. Please do not hesitate to ask for clarification in the comments, thanks!

Jsleshem
  • 715
  • 1
  • 10
  • 31

1 Answers1

1

you can try this

import ftplib
def getFile(filename,folder,ipaddr,usr,pswd):
        ftp = ftplib.FTP(ipaddr)
        ftp.login(usr,pswd)
        ftp.cwd(folder)
        ftp.retrbinary("RETR " + filename, open(filename, 'wb').write)
        ftp.quit()
Dimas Aps
  • 71
  • 8
  • Will try! Using filezilla for the moment as a proof of concept, but it is not scalable at all. Where do I put the filepath? – Jsleshem Sep 17 '21 at 16:41
  • 1
    'Folder' will be your ftp path. File will be save to your local folder, the location same with your. Py file. If you want to save to spesific location just modify "open( filename_with_location, 'wb'). Write – Dimas Aps Sep 18 '21 at 00:58