Currently my script looks for ESC in a specific .xlsx file name and gets the last characters after that which in my case is the date. The file looks like this: xxx_2392469513_1700001_ESC_2020_01.xlsx
filenames = os.listdir(os.path.join(path, path2, path3, path4))
for filename in filenames:
getdate = re.search('(?<=ESC_)\w+', filename)
#Replace '_' with '-'
if getdate:
date = getdate.group(0).replace('_', '-')
print('The following ESC file has date', date)
"The following ESC file has date 2020-01"
With this I get the date. However, I noticed that not every filename has the date after ESC i.e. xxx_2392469513_1700001_ESC_something_2020_01.xlsx. But it is crucial for me to only check the filename with an ESC in it.
How can I get the last 7 characters of that filename with re.search?