I'm trying to get latest new file in a directory of remote Linux server. The file in SFTP server is created every 4 hours and the file have specific name start with filegen_date_hour.json
as per example below. In this case latest file 'filegen_20200101_0800.json' need to be transferred to my local directory.
filegen_20200101_0000.json
filegen_20200101_0400.json
filegen_20200101_0800.json
I use Python 3 code below, but got error
latestFile = max(listFile, key=os.path.getctime)
ValueError: max() arg is an empty sequence
SFTP code below
myHostname = "192.168.100.10"
myUsername = "user"
myPassword = "password"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
with sftp.cd('/home/operation/genfiles/'):
fileDir = '/home/operation/genfiles/filegen_*.json'
**#file have specific pattern with filegen_*.json**
listFile = glob.glob(fileDir)
latestFile = max(listFile, key=os.path.getctime)
sftp.get(latestFile)
Appreciate help on this matter. Thank you for your response and help.