Download files from SFTP server that are older than 5 days using Python

前端 未结 1 1405
猫巷女王i
猫巷女王i 2021-01-06 21:53

I got a Python script on this site that downloads files from the directory from SFTP server. Now I need help to modify this code so that it only downloads the files that ol

1条回答
  •  被撕碎了的回忆
    2021-01-06 22:28

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

    Then, iterate the list and pick only the files you want.

    import time
    from stat import S_ISDIR, S_ISREG
    
    def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
        for entry in sftp.listdir_attr(remotedir):
            remotepath = remotedir + "/" + entry.filename
            localpath = os.path.join(localdir, entry.filename)
            mode = entry.st_mode
            if S_ISDIR(mode):
                try:
                    os.mkdir(localpath)
                except OSError:     
                    pass
                get_r_portable(sftp, remotepath, localpath, preserve_mtime)
            elif S_ISREG(mode):
                if (time.time() - entry.st_mtime) // (24 * 3600) >= 5:
                    sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)
    

    Though the code can be much simpler, if you do not need a recursive download:

    for entry in sftp.listdir_attr(remotedir):
        mode = entry.st_mode
        if S_ISREG(mode) and ((time.time() - entry.st_mtime) // (24 * 3600) >= 5):
           remotepath = remotedir + "/" + entry.filename
           localpath = os.path.join(localdir, entry.filename)
           sftp.get(remotepath, localpath, preserve_mtime=True)
    

    Based on:

    • Python pysftp get_r from Linux works fine on Linux but not on Windows
      (I have updated this source of your code to use listdir_attr as it's more effective)
    • How to sync only the changed files from the remote directory using pysftp?
    • Delete files that are older than 7 days

    0 讨论(0)
提交回复
热议问题