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. Edit: The format is actually mmddyy
.
How can I limit my downloads to only files created in the last 14 days? Is pysftp the best module for this?
question from:
https://stackoverflow.com/questions/66057375/how-can-i-only-download-files-with-timestamps-in-their-names-from-the-last-14-da 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…