python - How to download only the latest file from SFTP server with Paramiko? -


i want write script connects university sftp server , downloads latest file exercises. far i've changed little bit code paramiko example, not know how download latest file.

here code :

import functools import paramiko   class allowanythingpolicy(paramiko.missinghostkeypolicy):     def missing_host_key(self, client, hostname, key):         return  adress = 'adress' username = 'username' password = 'password'  client = paramiko.sshclient() client.set_missing_host_key_policy(allowanythingpolicy()) client.connect(adress, username= username, password=password)  def my_callback(filename, bytes_so_far, bytes_total):     print ('transfer of %r in progress' % filename)   sftp = client.open_sftp() sftp.chdir('/directory/to/file') filename in sorted(sftp.listdir()):     if filename.startswith('temat'):         callback_for_filename = functools.partial(my_callback, filename)         sftp.get(filename, filename, callback=callback_for_filename)  client.close()  

use sftpclient.listdir_attr instead of sftpclient.listdir listing attributes (including file timestamp).

then, find file entry greatest .st_mtime attribute.

the code like:

latest = 0 latestfile = none  fileattr in sftp.listdir_attr():     if fileattr.filename.startswith('temat') , fileattr.st_mtime > latest:         latest = fileattr.st_mtime         latestfile = fileattr.filename  if latestfile not none:     sftp.get(filename, filename) 

(the code may not 100% correct, not python)


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -