Reputation: 2543
I have c:\file_a.txt
which I would like to copy to /home/the_user/file_b.txt
. If the remote file exists already, replace it, if not, create it.
Here is my code:
import json
import paramiko
from dotenv import load_dotenv
from os import getenv
from os.path import join, dirname, expanduser
if __name__ == "__main__":
load_dotenv(join(dirname(__file__), ".env"))
ssh = paramiko.SSHClient()
ssh.load_host_keys(expanduser(join("~", ".ssh", "known_hosts")))
ssh.connect(getenv("SSH_SERVER"), username=getenv("SSH_USER"), \
password=getenv("SSH_PWD"), key_filename=getenv("SSH_KEY"))
sftp = ssh.open_sftp()
sftp.put("c:\file_a.txt", "/home/the_user/")
# my understanding is putting a file to a folder sets the working directory to that folder
sftp.rename("file_a.txt", "file_b.txt")
sftp.remove("file_a.txt")
sftp.close()
ssh.close()
But I am getting a Failure
error on the rename. Feels like I am missing something obvious and would appreciate any pointers.
How can I copy a local file to a remote file with a different filename over ssh/sftp/scp using the paramiko
library in Python
?
Upvotes: 1
Views: 1778
Reputation: 202168
So upload the file straight to the new name:
sftp.put("c:\file_a.txt", "/home/the_user/file_b.txt")
Upvotes: 1