-1

As you know from the title I am wondering if it is possible to transfer a folder using an FTP server. This would make it a lot easier to transfer multiple files all at once. Keep in mind however that the folder will likely contain other folders inside of it that all contain their own files. If this is possible please leave a response as to how I could achieve this. If it is not possible, is there any other method I could use rather than FTP?

Thanks!

Vulturu
  • 3
  • 3

1 Answers1

0
import os
from ftplib import FTP

def upload_dir(ftp, path):
    for name in os.listdir(path):
        local_path = os.path.join(path, name)
        if os.path.isfile(local_path):
            print(f"  uploading file {name} ...", end="")
            with open(local_path, 'rb') as file:
                ftp.storbinary(f'STOR {name}', file)
            print(" done.")
        elif os.path.isdir(local_path):
            print(f"making directory {name} ...", end="")
            try:
                ftp.mkd(name)
            except:
                print(" already exists.", end="")
            print(" navigating into ...", end="")
            ftp.cwd(name)
            print(" done.")
            upload_dir(ftp, local_path)
            print(f" navigating up ...", end="")
            ftp.cwd('..')
            print(" done.")


ftp = FTP('ftp.whatever.com')  # or your FTP server
ftp.login('USER', 'PW')  # login with your username and password

upload_dir(ftp, 'path/to/your/directory')  # your local directory path

ftp.quit()
SuperStew
  • 2,857
  • 2
  • 15
  • 27
  • Thank you so much @superstew. How can I download the uploaded folder because when I try to download it I get "ftplib.error_perm: 550 Permission denied." – Vulturu Jun 28 '23 at 23:21
  • Then you probably need to get the login part right. That will depend on the specific ftp server – SuperStew Jun 29 '23 at 13:21