使用Python将所有文件从一个SFTP文件夹存档到另一个文件夹 [英] Archive all files from one SFTP folder to another in Python
问题描述
我能够使用@Martin Prikryl [https://stackoverflow.com/questions/58719309/transfer-file-from-aws-s3-to提供的以下语法,将文件从S3成功上传到SFTP位置-sftp-using-boto-3/58725127]
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)
我需要先将当前文件从S3上载到archive
文件夹中.
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.
示例
我的文件夹如下,并且filename_20200623.csv
需要移动到ARCHIVE
文件夹,新文件filename_20200625.csv
将被上传.
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
预期
MKT
ABC
ARCHIVE
filename_20200623.csv
filename_20200625.csv
推荐答案
使用使用一步一步地移动它们 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)
对于将来使用Paramiko的读者来说,代码将是相同的,除了sftp
将引用 SFTPClient.listdir_attr
和 SFTPClient.rename
方法的行为相同到pysftp的那些.
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.
这篇关于使用Python将所有文件从一个SFTP文件夹存档到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!