Python Forum

Full Version: upload folder in server ftp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have some folder and subfolder and files in my computer like this:
[Image: architectureDossier.png]
and i want to upload this folders and files in my serveur ftp.
I have a code but is wrong because my folder go to the server ftp like that:
[Image: dossierSurFtp.png]
here is the code
import ftplib
import os

server = 'localhost'
username = 'generic_user'
password = 'password'
myFTP = ftplib.FTP(server, username, password)
myPath = r'/Users/olivier/Documents/essai'
def uploadThis(path):
    files = os.listdir(path)
    os.chdir(path)
    for f in files:
        if os.path.isfile(f):
            fh = open(f, 'rb')
            myFTP.storbinary('STOR %s' % f, fh)
            fh.close()
        elif os.path.isdir(f):
            myFTP.mkd(f)
            myFTP.cwd(f)
            uploadThis(f)
    myFTP.cwd('..')
    os.chdir('..')
uploadThis(myPath) 
Do you have to do this with ftplib? I would try paramiko for this task.
Hello
Thank you for your's answer, but i want to use ftplib.
There is this module pyftp in github. Its code is quite short and it has a method put_d() to upload a whole folder. Examining the code should give you the solution.