0

I am trying to write a program that will copy files from a local directory and put them in a directory on a remote server with python. I have looked into the paramiko and and subprocess libraries and none of them seem to work for me. If someone could show me how to do this I would appreciate it. It seems very straight forward but all of the methods I have seen online and on youtube do not work.

This command works for me to manually make the process work. command line command: scp -I ./privatekey //LocalIP/Home/APP/test.csv root@RemoteIP:/root/

Thank you.

1 Answers1

0

After some more research I was able to figure it out. Hopefully this helps other looking.

import pysftp
import os
sftpHost = 'remoteIP'
sftpPort = 22
user = 'user'
privateKeyFilePath = './privatekey'

directory = "I:\Dir\SubDir"

with pysftp.Connection(host=sftpHost, port=sftpPort, username=user, private_key=privateKeyFilePath) as sftp:
    #loop through directory and put files onto remote server
    for filename in os.listdir(directory):
        print(filename)
        sftp.put(f'{directory}/{filename}', preserve_mtime=True)