(Python) How do i search directories and find files that match regex?
<p>I recently started getting into Python and i am having a hard time searching through directories and matching files based on a regex that i have created. Basically i want it to scan through all the directories in another directory and find all the files that ends with .zip or .rar or .r01 and then run various commands based on what file it is. </p>

<pre><code>import os, re

rootdir = "/mnt/externa/Torrents/completed"

for subdir, dirs, files in os.walk(rootdir):
    if re.search('(w?.zip)|(w?.rar)|(w?.r01)', files):
        print "match: " . files
</code></pre>

<pre><code>import os
import re

rootdir = "/mnt/externa/Torrents/completed"
regex = re.compile('(.*zip$)|(.*rar$)|(.*r01$)')

for root, dirs, files in os.walk(rootdir):
  for file in files:
    if regex.match(file):
       print(file)
</code></pre>

<p><strong>CODE BELLOW ANSWERS QUESTION IN FOLLOWING COMMENT</strong></p>

<blockquote>
  <p>That worked really well, is there a way to do this if match is found on regex group 1 and do this if match is found on regex group 2 etc ? â nillenilsson</p>
</blockquote>

<pre><code>import os
import re

regex = re.compile('(.*zip$)|(.*rar$)|(.*r01$)')
rx = '(.*zip$)|(.*rar$)|(.*r01$)'

for root, dirs, files in os.walk("../Documents"):
  for file in files:
    res = re.match(rx, file)
    if res:
      if res.group(1):
        print("ZIP",file)
      if res.group(2):
        print("RAR",file)
      if res.group(3):
        print("R01",file)
</code></pre>

<p>It might be possible to do this in a nicer way, but this works. </p>

python linux directory