我编写了这个脚本来将文件从 SFTP 远程文件夹保存到本地文件夹。然后它从 SFTP 中删除该文件。我想更改它,以便它停止删除文件,而是将它们保存到 SFTP 上的备份文件夹中。我如何在 pysftp 中做到这一点?我找不到任何关于它的文档......
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
myHostname = "123"
myUsername = "456"
myPassword = "789"
with pysftp.Connection(host=myHostname, username=myUsername, password="789", cnopts=cnopts) as sftp:
sftp.cwd("/Production/In/")
directory_structure = sftp.listdir_attr()
local_dir= "D:/"
remote_dir = "/Production/"
remote_backup_dir = "/Production/Backup/"
for attr in directory_structure:
if attr.filename.endswith(".xml"):
file = attr.filename
sftp.get(remote_dir + file, local_dir + file)
print("Moved " + file + " to " + local_dir)
sftp.remove(remote_dir + file)
Run Code Online (Sandbox Code Playgroud)
不要担心我没有主机密钥或密码。一旦我让脚本工作,我就不会保持这种状态:)