olliepower
olliepower

Reputation: 1359

Python: function to identify all subfolders that match a pattern

I have written the following bit of code to find all subfolders matching a certain pattern. However I do not have a way of checking that this function actually is finding all matches.

I want to retrieve all folders that have a name of the form "19xx@60xx_npo" where xx are characters, possibly uppercase.

def findWrongEncut(path):
    pathList = glob.glob("./%s/19*@60*_npo" %path)
    print pathList
    print len(pathList)

Does the function above guarantee that I will get all folders that match "19xx@60xx_npo"

Upvotes: 3

Views: 2696

Answers (1)

kojiro
kojiro

Reputation: 77127

Yes, but it will also match things that are not directories and names like '19xxxxxx@60xxxxxxxx_npo'. If you want to match a specific number of letters, use ? for each character in your glob. If you want to guarantee directories, throw a trailing slash on the glob expression:

pathList = glob.glob("./%s/19??@60??_npo/" % path)

Upvotes: 7

Related Questions