简体   繁体   中英

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

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

pattern = "SALES"

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM