Ashish Bainade
Ashish Bainade

Reputation: 506

issue in pattern matching in python using glob

I have directory having file names "VISCUS","MISMANAGE" etc i want to find files matching given pattern.

pattern = "SALES" # this changes dynamically as command-line args I am doing like below

import glob
files_present = glob.glob(r"*pattern*")
#glob.glob(r"*SALES*")works okay

Upvotes: 0

Views: 426

Answers (2)

hiro protagonist
hiro protagonist

Reputation: 46859

something like this?

for pattern in ("VISCUS", "MISMANAGE"):
    files_present = glob.glob(r"*{}*".format(pattern))
    # do stuff with present files

you create the glob string from the loop variable.

Upvotes: 0

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12140

'*pattern*' will not change to '*SALES*'. Try this:

pattern = "SALES"

import glob
files_present = glob.glob(r"*{}*".format(pattern))

Upvotes: 1

Related Questions