Skip to content

Instantly share code, notes, and snippets.

@josh-gree
Last active January 29, 2020 21:06
Show Gist options
  • Save josh-gree/3d5e410de03ff4b1ec6cc685553472bc to your computer and use it in GitHub Desktop.
Save josh-gree/3d5e410de03ff4b1ec6cc685553472bc to your computer and use it in GitHub Desktop.
def get_latest_file_from_sftp(*, local_file_location: str, config: dict) -> None:
host = config["host"]
password = config["password"]
username = config["username"]
base_folder = config["base_folder"]
conn = pysftp.Connection(host, username=username, password=password)
fnames = conn.listdir(base_folder)
fnames = [
(
fname,
datetime.fromtimestamp(
conn.lstat(f"{base_folder}/{fname}").st_mtime
),
)
for fname in fnames
]
most_recent_file = max(fnames, key=lambda x: x[1])[0]
logger.info(f"Most recent file: {most_recent_file}")
conn.get(f"{base_folder}/{most_recent_file}",local_file_location)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment