Python 如何按修改日期对使用Paramiko从SFTP服务器提取的文件列表进行排序?

Python 如何按修改日期对使用Paramiko从SFTP服务器提取的文件列表进行排序?,python,python-3.x,sftp,paramiko,Python,Python 3.x,Sftp,Paramiko,我有一段代码,它使用Paramiko从服务器中提取文件。如何按修改日期对这些文件进行排序 ssh = paramiko.SSHClient() # automatically add keys without requiring human intervention ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() ) ssh.connect(sftpURL, username=sftpUser, password=sftpPa

我有一段代码,它使用Paramiko从服务器中提取文件。如何按修改日期对这些文件进行排序

ssh = paramiko.SSHClient()
# automatically add keys without requiring human intervention
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )

ssh.connect(sftpURL, username=sftpUser, password=sftpPass)

sftp = ssh.open_sftp()
filesInSFTP = sftp.listdir(sftpPullDirectory)
# Get only the XML and XLSX files
filesInSFTP = [file for file in filesInSFTP if file.lower().endswith(('.xml', '.xlsx'))]

使用检索具有文件属性(包括修改时间)的列表。然后对列表进行排序


相关问题:


强制性警告:不要使用
AutoAddPolicy
–这样做会失去保护。有关正确的解决方案,请参阅

filesInSFTP = sftp.listdir_attr(sftpPullDirectory)
filesInSFTP.sort(key = lambda f: f.st_mtime)