I need to check file extension when search over a directory.
if using re to do the matching work. those ‘.’ is interpreted as regex ‘.’
my code:
extension = ['.c','.h']
path = 'foo\bar\foobar.c'
def skipCheck(path):
global extension
skip = True
for i in extension :
if(re.search(i,path)):
skip = False
return skip
I know I could use backslash to do this.
extension = ['\.c','\.h']
But it is not easy to use and configure.I want to keep the [‘.c’,’.h’] input style.
Is there a way to convert and save them to another list of raw string for re.search.
Don’t use regexen; Python already has
os.path.splitext.If you really must use a regex, you can call
re.escapeto escape all regex metacharacters.Don’t declare
extensionglobal; you’re not assigning to it so you don’t need to. Also, you should call itextensions.