Reputation: 4428
I need to put multiple file on SFTP server. But I am confused here of how to loop through them if path to SFTP requires file name and extension? Which in my case it will be dynamic.
# sftp path where file should be delivered
sftp_path = fr'/Incoming/{filename_PRE}.csv' # if not supply filename it will give permission error. Why?
## push file to sftp server
def push_file_sftp():
try:
## HostKey
keydata = b"""AAAABBB2222888555="""
key = paramiko.RSAKey(data=decodebytes(keydata))
cnopts = sftp.CnOpts()
cnopts.hostkeys.add('hostname', 'ssh-rsa', key)
## Credentials
s = sftp.Connection(host='hostname', username='username', password='psswd',cnopts=cnopts)
# how can I put 2 files here?
s.put(filepath_PRE, sftp_path)
s.close()
except Exception as e:
print(str(e))
push_file_sftp()
Upvotes: 1
Views: 1660
Reputation: 202292
So just use update the remote path with the correct filename:
sftp_path = '/Incoming'
filepath_PRE = "file1"
s.put(filepath_PRE, sftp_path + "/" + filepath_PRE + ".csv")
filepath_PRE = "file2"
s.put(filepath_PRE, sftp_path + "/" + filepath_PRE + ".csv")
Upvotes: 1