Python - Loop through files of certain extensions

You can use os.path.splitext which takes a path and splits the file extension from the end of it:

import os
rootdir = 'input'
extensions = ('.mp4', '.avi', '.wmv')

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        ext = os.path.splitext(file)[-1].lower()
        if ext in extensions:
            print (os.path.join(subdir, file))

For multiple extensions, the simplest is just to use str.endswith passing a tuple of substrings to check:

  for file in files:
      if file.endswith((".avi",".mp4","wmv")):
         print (os.path.join(subdir, file))

You could use iglob like below and chain the searches returned or use re.search but using endswith is probably the best approach.

from itertools import chain
from glob import iglob

for subdir, dirs, files in os.walk(rootdir):
    for file in chain.from_iterable(iglob(os.path.join(rootdir,p)) for p in ("*.avi", "*.mp4", "*wmv")) :
            print(os.path.join(subdir, file))

Using python3.5 glob now supports recursive searches with the ** syntax:

from itertools import chain
from glob import iglob

from glob import iglob
for file in chain.from_iterable(iglob(os.path.join(rootdir,p)) 
      for p in (rootdir+"**/*.avi", "**/*.mp4", "**/*wmv")):
          print(file)

I actually did something similar to this a couple of days ago and here is how I did it:

EXTENSIONS = ('.cpp','.hpp')

for root, dirs, files in os.walk(top):
    for file in files:
        if file.endswith(EXTENSIONS):
            #file which ends with extension type so do your thing!

Hope this is what you are after. You can see the whole script here on my github.