Search code examples
stringpython-3.xwildcardfilenames

Wildcard in string as input


I have a variable that is an input for a process. Its essentially the full path name of a file, but injects a value based on a list to get the correct name:

 fipsList = ['06001','06037','06059']
 for fip in fipsList:
     file = r"T:\CCSI\TECH\FEMA\Datasets\NFHL\NFHL_06122018\NFHL_{}_20180518.gdb".format(fip)"

What I want to do now is make everything between "...NFHL_{}_ and ....gdb" to be a wildcard "*". Simply using file = r"T:\CCSI\TECH\FEMA\Datasets\NFHL\NFHL_06122018\NFHL_{}_*.gdb".format(fip)" doesn't seem to work. Essentially, this is what that produces: >>>'T:\\CCSI\\TECH\\FEMA\\Datasets\\NFHL\\NFHL_06122018\\NFHL_06_*.gdb'. Suggestions on how to get it to work?


Solution

  • Simply adding '*' into a string this way will not work. The set up of the question is poor (my own fault), but for clarification's sake, here's how I resolved the issue:

     fipsList = ['06001','06037','06059']
     for fip in fipsList:
         path = r"T:\CCSI\TECH\FEMA\Datasets\NFHL\NFHL_06122018"
         for root, dirs, filename in os.walk(path):
            for dir in dirs:
                if('NFHL_' + fip[:2] in dir and '.gdb' in dir):
                    file = os.path.join(root, dir)
    

    Essentially, I had to walk through the folder and use an if conditional to make sure that the conditions of having both the fip value and the .gdb extension were met.