Python Regex: Finding files without a certain extension -
i'm trying figure out regular expression use in order search directory , return number of files in directory not have prefix 'abc_'. e.g, in directory files def_notes.txt, abc_notes.txt, ghi_notes.txt, function recognize there 2 files without 'abc_' prefix , return 2.
to point, i'm having trouble writing regular expression represent this. i've tried re.compile('^(.(?!(abc_)))*$') found here. however, doesn't seem work. leaving first dot inside parenthesis matches file 'abc_notes.txt'. if remove dot, won't match 'abc_notes.txt', doesn't match 'def_notes.txt'.
edit: clarify, i'll use glob or os packages work once figure out expression. i'm using re.compile , search in python shell figure out regex.
this code prints out how many files in current directory don't start letter z
:
import glob, re print len( [path path in glob.glob('*') if not path.startswith('z') ] )
the following code uses regular expression. matches letter 'z' @ beginning of string -- reverses match (?!...)
syntax.
pat = re.compile('^(?!z)') print len( filter(none, map(pat.match, glob.glob('*'))) )
Comments
Post a Comment