Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

use glob package. allows multiple usage of regexes

import glob
dir = 'path/to/files/'
flist = glob.glob(dir + 'T*Mhz*')
print(flist)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading