1

I know how to connect with SFTP server and get the newest file:

with pysftp.Connection(host=host, username=user, password=pass, cnopts=cnopts) as sftp:
    print("Connected")

    sftp.cwd('/path')

    latest = 0
    latestfile = None

    for fileattr in sftp.listdir_attr():
        if fileattr.filename.startswith('Name') and fileattr.st_mtime > latest:
            latest = fileattr.st_mtime
            latestfile = fileattr.filename

    if latestfile is not None:
        localFilePath = '//path/to/download/file.txt'
        sftp.get(latestfile, localFilePath)

Now I need to find in /path the folder whose name has yesterday's timestamp in format dd.mm.yyyy and download the newest file from it.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
MRO
  • 133
  • 7

1 Answers1

1

Just enter the yesterday's folder:

from datetime import datetime, timedelta

yesterday = datetime.now() - timedelta(1)
name = datetime.strftime(yesterday, '%d.%m.%Y')

sftp.cwd('/path/' + name)

Based on Python - Get Yesterday's date as a string in YYYY-MM-DD format.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992