Making efficient code using re.findall to sort files in folder

This is how the files are named in the folder :

Data20210608_FL_.xlsx
Data20210608_FLFR_.xlsx
Data20210510-fl_.xlsx
Data20210510-flfr_.xlsx
Data20210608_LRC_.xlsx
Data20210609_LRC_.xlsx

I would like to:

  1. use a loop to open only the ones containing FL or FLFR
  2. separate between the ones ending with FL and the ones ending with FLFR;

This is my code but it does not work and I don't fully understand how to use re.findall

 import glob
 import os
 import re
 import pandas as pd

 # %%
 directory = r'C:/ .../Licor/'
 appendix = "_.xlsx"
 location = directory + appendix

 datafinal = pd.DataFrame()

 #%%
 for filepath in glob.iglob(location):
     print(filepath)

     head_tail = os.path.split(filepath)

     Treatment = re.findall("[_FL][^_]*", head_tail[1])[0] 
     data = pd.read_excel(filepath)

     data['Spectrum']= Treatment
     datafinal= pd.concat([datafinal, data])

Thank you!

1 answer