Reputation: 139
I have a python application in which I have defined some tools with extensions.
extList = ["*pdf","*.cod","*log*","*.log.*"]
Now how to return files based on these extensions.
I used endWith() string method to match extension but it will not follow patterns.
extList = ["*pdf","*.cod","*log*","*.log.*"]
if os.path.isfile(unicode(path)):
if unicode(path).endswith(tuple(extList)):
print("match")
how to write regEx to match following patterns in single RegEx ? please suggest RegEx.
How to Write RegEx to Match File extension patterns like below:-
*.pdf
*.pdf*
*.pdf.*
*.pdf*.*
Upvotes: 0
Views: 140
Reputation: 18940
What you want is fnmatch
module instead of glob
:
Example:
import glob
extList = ["*pdf","*.cod","*log*","*.log.*"]
any(fnmatch.fnmatch(path, ext) for ext in extList)
Test:
>>> any(fnmatch.fnmatch(u'test.pdf', ext) for ext in extList)
True
>>> any(fnmatch.fnmatch(u'test.zip', ext) for ext in extList)
False
Upvotes: 1