Jonnyboi
Jonnyboi

Reputation: 557

Python: Glob.Glob wildcard matching, loop through list for wildcard

I am trying to use the glob.glob wildcard name to pickup files with particular names.

listing = ['DBMP','CIFP']

for x in range(len(listing)): 
    print(listing[x]) 
       
pklFilenamesList = glob.glob(os.path.join(input_location, '*{x}_.pkl'))

How can I loop through my listing strings and concatenate that with _.pkl files names for my pklFilenamesList variable?

I want to create a list like so :

*DBMP_.pkl 
*CIFP_.pkl

Update, I am trying to loop through the list like below :

pklFilenamesList = glob.glob(os.path.join(input_location, '*{x}_.pkl'))

for filecounter, filename in enumerate(pklFilenamesList):

Upvotes: 0

Views: 412

Answers (1)

user16836078
user16836078

Reputation:

For the list part, you can easily get the result using a list comprehension with f-string.

newlist = [f'*{x}_.pkl' for x in listing]

['*DBMP_.pkl', '*CIFP_.pkl']

Update

After getting the newlist, you can just loop over it and append the result into a list.

pklFilenamesList = []

for i in newlist:
    pklFilenamesList.append(glob.glob(os.path.join(input_location, i )))

for j in pklFilenamesList:
    for filecounter, filename in enumerate(j):
        ...

or you can continue with what you need to do without appending it.

Upvotes: 1

Related Questions