Home > Mobile >  (python) read specific type of .xlsx file name in a folder
(python) read specific type of .xlsx file name in a folder

Time:01-05

I search a few related discussions, such as enter image description here

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!

CodePudding user response:

use glob package. allows multiple usage of regexes

import glob
dir = 'path/to/files/'
flist = glob.glob(dir   'T*Mhz*')
print(flist)
  •  Tags:  
  • Related