1

I got a code to download 5 days older files from SFTP server. But instead of downloading the files, I want to store the names of 5 days older files into a list. Please help me to modify the code. Thanks in advance

Code I am using right now (based on Download files from SFTP server that are older than 5 days using Python)

import time

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)
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
udit kanotra
  • 328
  • 1
  • 3
  • 17

1 Answers1

1

instead of using sftp.get to download the files, I'm just adding their path to a list, and returning it at the end

import time

def get_r_portable(sftp, remotedir):
    result = []
    for entry in sftp.listdir_attr(remotedir):
        remotepath = remotedir + "/" + entry.filename
        mode = entry.st_mode
        if S_ISDIR(mode):
            result += get_r_portable(sftp, remotepath)
        elif S_ISREG(mode):
            if (time.time() - entry.st_mtime) // (24 * 3600) >= 5:
                result.append(entry.filename)
    return result
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
  • 1
    hey thanks for your efforts. bro your code is working but it appends filename with path like this "['/home/atpl/hello.txt'] . Can please suggest a method so that it only appends file name like "['hello.txt']" excluding path – udit kanotra May 28 '19 at 11:56
  • 1
    of course, edited my answer so now only the filename is added (without full path) – Adam.Er8 May 28 '19 at 11:59
  • @ Adam.Er8 your code stores filenames inside a subfolder present in remote directory. Can you help me with this code so that it stores the name of subfolders present on remote directory instead of filenames inside it? – udit kanotra May 29 '19 at 07:59