I need to download all files in our SFTP with a modified date of yesterday. This is my most recent attempt. I'm sort of convinced it has something to do with the time stamp, but all attempts to strip the timestamp off both variables haven't fixed my problem. The below code downloaded 11 of 14 files with a mod date of yesterday timestamps between 12:29:05 AM and 2:28:46 AM. The 3 missing files had timestamps 11:35:25 through 11:29:34. I am using SQL Server Agent to schedule the run, and the server runs the script as the same service account I have hard coded into the script.
from os.path import basename
import os.path
import paramiko
import datetime
from datetime import date, timedelta
#Assigning variables to upload file to SFTP
host = 'SERVER'
port = PORT
transport = paramiko.Transport((host, port))
##setting up transport
username = 'UNAME'
password = 'PW'
##connecting to FTP
transport.connect(hostkey = None, username = username, password = password,pkey = None)
sftp = paramiko.SFTPClient.from_transport(transport)
##create yesterday var and normalize it
yesterday = date.today() - timedelta(days=1)
yDay = yesterday.day
yYear = yesterday.year
yMonth = yesterday.month
##create a list of all files and get name/modified date of the fiels.
for file in sftp.listdir_attr('WORKING DIRECTORY'):
i = file.st_mtime
filedate = date.isoformat(i)
fdDay = filedate.day
fdYear = filedate.year
fdMonth = filedate.month
if fdDay == yDay and fdYear == yYear and fdMonth == yMonth:
remotepath = 'WORKING DIRECTORY' + file.filename
localpath = 'LOCAL PATH' + file.filename
sftp.get(remotepath, localpath)
#Close Connection
sftp.close()
transport.close()