2021年2月4日星期四

How can I only download files created in the last 14 days from an SFTP - instead of downloading the entire folder?

import os  import datetime  from datetime import datetime  from dateutil.relativedelta import relativedelta  from dateutil import parser  import pysftp    lt_all = []    # disable hostkey checking  cnopts = pysftp.CnOpts()  cnopts.hostkeys = None    lt_all = []    srv = pysftp.Connection('sftp.com', username = 'username', password = "password", cnopts = cnopts)  srv.chdir('download')  server_file_list = srv.listdir()    for lt_file in server_file_list:      if srv.isfile(lt_file) and ('invoices' in lt_file.lower() and 'daily' in lt_file.lower() and lt_file.endswith('.csv')):          try:               srv.get(lt_file,os.path.join(os.path.join(data_folder_path,'Invoices'),lt_file),preserve_mtime=True)          except:              print("No Invoices Today")  

The good news: I have been successfully downloading all CSV files from the SFTP location.

The bad news: all CSV files are being downloaded. Downloading 300+ files everyday is sub-optimal because downloading files that have already been downloaded is redundant.

These CSV files are generated daily. These files follow the same naming convention everyday: invoices_daily_20200204.csv. Notice the date comes at the very end in yyyymmdd format.

How can I limit my downloads to only files created in the last 14 days? Is pysftp the best module for this?

https://stackoverflow.com/questions/66057375/how-can-i-only-download-files-created-in-the-last-14-days-from-an-sftp-instead February 05, 2021 at 11:58AM

没有评论:

发表评论