>我需要在SFTP服务器上放置多个文件。但是我在这里感到困惑,如果SFTP的路径需要文件名和扩展名,如何遍历它们?就我而言,这将是动态的。
# 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()
因此,只需使用正确的文件名更新远程路径:
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")