Move Files from One Directory to Another with Paramiko

Move files from one directory to another with Paramiko

Use the sftp.rename:

sftp.rename(remotePath+'/tmp/'+fileName, remotePath+fileName)

Note that some SFTP servers fail the request, if the source and target directories are on different file systems.


If you need to move set of files from one folder to another, see:

Archive all files from one SFTP folder to another in Python

Directory transfers with Paramiko

You'll need to do this just like you would locally with python (if you weren't using shutils).

Combine os.walk(), with sftp.mkdir() and sftp.put(). You may also want to check each file and directory with os.path.islink() depending on whether you want to resolve symlinks or not.

Copy file from remote dir to remote sub directory using paramiko

You can try in the below way

   a=paramiko.SSHClient()
a.set_missing_host_key_policy(paramiko.AutoAddPolicy())
a.connect('ip',username='user',password='passw')
stdin, stdout, stderr = a.exec_command("cp /sourc/file /target/file")

Copy a file to server with a different filename using Python Paramiko over ssh/sftp

So upload the file straight to the new name:

sftp.put("c:\file_a.txt", "/home/the_user/file_b.txt")

Python moving file on SFTP server to another folder

Use Connection.rename:

sftp.rename(remote_dir + file, remote_backup_dir + file)

Obligatory warnings:

  • Do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.
  • Do not use pysftp. It's dead. Use Paramiko. See pysftp vs. Paramiko.


Related Topics



Leave a reply



Submit