python sftp 删除_使用Python将所有文件从一个SFTP文件夹归档到另一个

I was able to successfully upload the file from S3 to SFTP location using the below syntax as given by @Martin Prikryl [https://stackoverflow.com/questions/58719309/transfer-file-from-aws-s3-to-sftp-using-boto-3/58725127]

with sftp.open('/sftp/path/filename', 'wb') as f:

s3.download_fileobj('mybucket', 'mykey', f)

I have a requirement to archive the previous file into the archive folder from the current folder before uploading the current dated file from S3 to SFTP

I am trying to achieve using the wildcard, because sometimes, when running on Monday, you won't be able to find the file for Sunday and you have the previous file which is Friday's file. So I want to achieve any of the previous file irrespective of the date.

Example

I have folder as below and filename_20200623.csv needs to be moved to ARCHIVE folder and the new file filename_20200625.csv will be uploaded.

MKT

ABC

ARCHIVE

filename_20200623.csv

Expected

MKT

ABC

ARCHIVE

filename_20200623.csv

filename_20200625.csv

解决方案

Use Connection.listdir_attr to retrieve list of all files in the directory, filter it to those you are interested in, and then move them one-by-one using Connection.rename:

remote_path = "/remote/path"

archive_path = "/archive/path"

for f in sftp.listdir_attr(remote_path):

if (not stat.S_ISDIR(f.st_mode)) and f.filename.startswith('prefix'):

remote_file_path = remote_path + "/" + f.filename

archive_file_path = archive_path + "/" + f.filename

print("Archiving %s to %s" % (remote_file_path, archive_file_path))

sftp.rename(remote_file_path, archive_file_path)

For future readers, who use Paramiko, the code will be identical, except of course that sftp will refer to Paramiko SFTPClient class, instead of pysftp Connection class. As Paramiko SFTPClient.listdir_attr and SFTPClient.rename methods behave identically to those of pysftp.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值