I would open the file based on the modification time on the machine's file system.
This involves doing a recursive file-list, then calling stat()
on each file to retrieve the last-modified date:
EDIT: I mis-read the question, you actually want the newest file (I was finding the oldest)
import os
import sys
import glob
DIRECTORY='.'
### Builds a recursive file list, with optional wildcard match
### sorted so that the oldest file is first.
### Returns the name of the oldest file.
def getNewestFilename(path, wildcard='*'):
age_list = []
# Make a list of [ <modified-time>, <filename> ]
for filename in [y for x in os.walk(path) for y in glob.glob(os.path.join(x[0], wildcard))]:
modified_time = os.stat(filename).st_mtime
age_list.append([modified_time, filename])
# Sort the result, oldest-first
age_list.sort(reverse=True)
if (len(age_list) > 0):
return age_list[0][1]
else:
return None
latest_file = getNewestFilename(DIRECTORY, 'StockPriceReport*.pdf')
if (latest_file != None):
print("Newest file is [%s]" % (latest_file))
data = open(latest_file, "rb").read()
# ...
else :
print("No Files")