使用Python从SFTP服务器下载超过5天的文件 [英] Download files from SFTP server that are older than 5 days using Python

查看:263
本文介绍了使用Python从SFTP服务器下载超过5天的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此站点上获得了一个Python脚本,该脚本可从SFTP服务器从目录下载文件.现在,我需要帮助来修改此代码,以便仅下载使用该代码之日起5天之前的文件.

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 older than 5 days from the day the code is used.

用于下载文件的代码(基于来自Linux的Python pysftp get_r在Linux上运行良好,但在Windows上无法运行):

Code to download files (based on Python pysftp get_r from Linux works fine on Linux but not on Windows):

import os
import pysftp
from stat import S_IMODE, S_ISDIR, S_ISREG

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None    
sftp=pysftp.Connection('192.168.X.X', username='username',password='password',cnopts=cnopts)

def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
    for entry in sftp.listdir(remotedir):
        remotepath = remotedir + "/" + entry
        localpath = os.path.join(localdir, entry)
        mode = sftp.stat(remotepath).st_mode
        if S_ISDIR(mode):
            try:
                os.mkdir(localpath,mode=777)
            except OSError:     
                pass
            get_r_portable(sftp, remotepath, localpath, preserve_mtime)
        elif S_ISREG(mode):
            sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)

remote_path=input("enter the remote_path: ")
local_path=input("enter the local_path: ")

get_r_portable(sftp, remote_path, local_path, preserve_mtime=False)

请帮助我修改代码,使其仅下载比当前日期早5天的文件.

Please help me to modify the code so that it only download files 5 days older from the present day.

推荐答案

使用 pysftp.Connection.listdir_attr 以获得具有属性(包括文件时间戳记)的文件列表.

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

这篇关于使用Python从SFTP服务器下载超过5天的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆