lanneke113
lanneke113

Reputation: 53

Python selecting files by last letter and extension

I have two files in a folder: with name_E.mat and name_N.mat. How can I select them by the last letter+extension? The following code is what I tried:

filedir = r'C:\Users\320037415\Documents\Depth\Proefpersonen\Sub70'
enterprise = glob.glob(filedir + "/_E.mat")
neolead = glob.glob(filedir + "/_N.mat")
filelist = neolead + enterprise

Upvotes: 2

Views: 567

Answers (2)

Rakesh
Rakesh

Reputation: 82765

Using the os module.

Demo:

import os

filedir = r'C:\Users\320037415\Documents\Depth\Proefpersonen\Sub70'
filelist = [file for file in os.listdir(filedir) if file.endswith(("_E.mat", "_N.mat")) ]

Upvotes: 0

blhsing
blhsing

Reputation: 106553

You can use * as a wildcard for file names:

enterprise = glob.glob(filedir + "/*_E.mat")
neolead = glob.glob(filedir + "/*_N.mat")

Upvotes: 2

Related Questions