Python 将 SFTP 服务器上的文件移动到另一个文件夹
Python moving file on SFTP server to another folder
我编写此脚本是为了将文件从 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)
不用担心我没有主机密钥或明文密码。一旦脚本开始工作,我就不会再这样了:)
sftp.rename(remote_dir + file, remote_backup_dir + file)
强制警告:不要设置cnopts.hostkeys = None
,除非你不关心安全。有关正确的解决方案,请参阅 Verify host key with pysftp.
我编写此脚本是为了将文件从 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)
不用担心我没有主机密钥或明文密码。一旦脚本开始工作,我就不会再这样了:)
sftp.rename(remote_dir + file, remote_backup_dir + file)
强制警告:不要设置cnopts.hostkeys = None
,除非你不关心安全。有关正确的解决方案,请参阅 Verify host key with pysftp.