I search a few related discussions, such as Read most recent excel file from folder PYTHON however, it does not fit my requirement quite well.
Suppose I have a folder with the following .xlsx files
I want to read the files with name "T2xxMhz", i.e., the last 7 files.
I have the following codes
import os
import pandas as pd
folder = r'C:\Users\work' # <--- find the folder
files = os.listdir(folder) # <--- find files in the folder 'work'
dfs ={}
for i, file in enumerate(files):
if file.endswith('.xlsx'):
dfs[i] = pd.read_excel(os.path.join(folder,file), sheet_name='Z=143', header = None, skiprows=[0], usecols = "B:M") # <--- read specific sheet with the name 'Z=143'
num = i + 1 # <--- number of files.
However in this codes, I cannot differentiate two types of file name 'PYTEST' and 'T2XXX'.
How to deal with this problem? Any suggestions and hints please!