从SFTP文件夹下载最新的文件,其中包含昨天';在Python中以其名称标记时间戳
我知道如何连接SFTP服务器并获取最新文件:从SFTP文件夹下载最新的文件,其中包含昨天';在Python中以其名称标记时间戳,python,python-3.x,sftp,pysftp,Python,Python 3.x,Sftp,Pysftp,我知道如何连接SFTP服务器并获取最新文件: with pysftp.Connection(host=host, username=user, password=pass, cnopts=cnopts) as sftp: print("Connected") sftp.cwd('/path') latest = 0 latestfile = None for fileattr in sftp.listdir_attr():
with pysftp.Connection(host=host, username=user, password=pass, cnopts=cnopts) as sftp:
print("Connected")
sftp.cwd('/path')
latest = 0
latestfile = None
for fileattr in sftp.listdir_attr():
if fileattr.filename.startswith('Name') and fileattr.st_mtime > latest:
latest = fileattr.st_mtime
latestfile = fileattr.filename
if latestfile is not None:
localFilePath = '//path/to/download/file.txt'
sftp.get(latestfile, localFilePath)
现在,我需要在
/path
中找到名称具有昨天时间戳的文件夹,格式为dd.mm.yyyy
,并从中下载最新文件。只需输入昨天的文件夹:
from datetime import datetime, timedelta
yesterday = datetime.now() - timedelta(1)
name = datetime.strftime(yesterday, '%d.%m.%Y')
sftp.cwd('/path/' + name)
基于