How can this python regex file finder be improved? I've just spent a good hour or so developing a python script that hunts down and removes .php files depending on the regex expression the situation was a client has got several sites and got hacked recently and as a result a large portion of 8 character random php files have created, so in order to remove them all I made a python script to hunt and find the files whilst avvoiding the files needed also which are 8 characters.
Target:
n1qv0iop.php
e7wo0sb8.php
sdt24g9o.php
9ktrhstu.php
2cvgc7qa.php
nlrgnwzt.php
pyj67osr.php
Script:
import os
import re

rootdir = &quot;/ROOTDIRECTORY&quot;
regex = re.compile('^((((?!absolute|abstract|accounts|activate|activity|advanced|archives|autoload|autoplay|basiccss|bookmark|calendar|captions|category|checkbox|comments|compiled|conntest|controls|counters|courierb|courieri|creative|cronview|database|defaults|disabled|document|dropcaps|elements|embedded|error404|external|facebook|features|fieldset|flamingo|flipcard|freedoms|frontend|getcombo|getnodes|groupped|gzdecode|homepage|icongrid|iconlist|igniteup|importer|includer|launcher|lazyload|licenses|lightbox|location|magazine|mailpoet|manifest|mappings|myticket|optimize|packager|password|phpthumb|platform|pointers|products|progress|promobox|radiotab|redirect|register|renderer|repeater|required|response|revision|richtext|rollback|rollover|security|services|settings|sidebars|sitemaps|switcher|taxonomy|template|testmail|textarea|thankyou|timeline|tracking|upgrader|upgrades|variable|videobox|whatsnew|payments|donation|checkout|readmore|overview|tooltips|purchase|endpoint|firewood|pagepost|jwplayer|analytic|builders|bulkedit|carousel|delivery|featured|feedback|fontello|fontpack|headline|honeypot|messages|metadata|position|registry|sendaway|sections|singular|siblings|subpages|tabgroup|thickbox|turfmail|webfonts|networks|htaccess))[a-z0-9]{8})).php$')

for root, dirs, files in os.walk(rootdir):
  for file in files:
    if regex.match(file):
   print(os.path.join(root,file))
   #os.remove(os.path.join(root,file))

   print &quot;END&quot;;

My question is can this be improved? is there a better way to write the regex pattern?just looking for advice :)
I think a regex is overkill here. There comes a point where evaluating all these or'ed exceptions becomes just too slow. I would instead use a simple glob and a blacklist:
from pathlib import Path

IGNORE = {&quot;absolute&quot;, &quot;abstract&quot;, ...}

root = Path(&quot;/ROOTDIRECTORY&quot;)
for file in root.glob(&quot;**/*.php&quot;):
    if len(file.stem) == 8 and file.stem not in IGNORE:
        print(file)

Note that I used a set for a fast check with in.
You could also use the glob &quot;**/????????.php&quot;, as suggested by @RootTwo in the comments, then you wouldn't have to do the length check, although that is quite fast.
Indeed, what you should ideally do is not take my or anyone else's word for it, but measure. Measure how fast your approach is, how fast an optimized regex is, how fast mine is and how fast using a different glob is. Maybe it matters, or maybe all of this is premature optimization and it doesn't actually matter that one takes a millisecond and the other a second (or whatever the actual results are). That depends entirely on your use case.