如何使用 Python 中的 pysftp "put" 方法将多个 .csv 文件传送到 SFTP 服务器
How to deliver multiple .csv files to SFTP server using pysftp "put" method in Python
我需要将多个文件放在 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")
我需要将多个文件放在 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")