Raja Simon
Raja Simon

Reputation: 10305

paramiko upload folder via sftp

Inside my working code, i have this

import paramiko
parent=os.path.split(dir_local)[1]
for walker in os.walk(parent):
    try:
        self.sftp.mkdir(os.path.join(dir_remote,walker))
    except:
        pass
for file in walker[2]:
    sftp.put(os.path.join(walker[0],file),os.path.join(dir_remote,walker[0],file))

the error now showing is

Trying ssh-agent key 5e08bb83615bcc303ca84abe561ef0a6 ... success
Caught exception: <type 'exceptions.IOError'>: [Errno 2] Directory does not exist.

The print walker showing all the files inside that folder but i dont know why the folder doesn't copy to sftp server

Upvotes: 1

Views: 4601

Answers (1)

monkut
monkut

Reputation: 43832

unless you've overridden os.walk() it yields a tuple of three objects: dirpath, dirnames, filenames

So, you're os.path.join(dir_remote, walker) call will always throw an exception, resulting in your expected directory not being created.

I find it's clearer to write the os.walk() loop like this:

for dirpath, dirnames, filenames in os.walk(parent):
    remote_path = os.path.join(dir_remote, dirpath)
    # make remote directory ...
    for filename in filenames:
        local_path = os.path.join(dirpath, filename)
        remote_fliepath = os.paht.join(remote_path, filename)
        # put file

Keep in mind that os.walk() will walk any directories in your given parent.

Upvotes: 6

Related Questions