How to sync only the changed files from the remote directory using pysftp?

别等时光非礼了梦想. 提交于 2019-11-29 12:11:59

Use the pysftp.Connection.listdir_attr to get file listing with attributes (including the file timestamp).

Then, iterate the list and compare against local files.

import os
import pysftp
import stat

with pysftp.Connection('example.com', username='username', password='password') as sftp:
    sftp.cwd("/remote/path")
    for f in sftp.listdir_attr():
        if not stat.S_ISDIR(f.st_mode):
            print("Checking %s..." % f.filename)
            if ((not os.path.isfile(f.filename)) or
                (f.st_mtime > os.path.getmtime(f.filename))):
                print("Downloading %s..." % f.filename)
                sftp.get(f.filename, f.filename)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!