19
votes

I'm trying to loop through a folder and all subfolders to find all files of certain file types - for example, only .mp4, .avi, .wmv.

Here is what I have now, it loops through all file types:

import os
rootdir = 'input'

for subdir, dirs, files in os.walk(rootdir):
     for file in files:
          print (os.path.join(subdir, file))

Thanks!

4
For beginners who came here through search, note that the OP's code recurses through subdirectories and finds all files with a particular extension in an entire directory tree (folders withing folders within folders etc within a starting folder). - tripleee

4 Answers

21
votes

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))
24
votes

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)
3
votes

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.

0
votes

Since Python 3.4 you can use pathlib:

from pathlib import Path
from itertools import chain

rootdir = 'input'
p = Path(rootdir)
for file in (chain(p.glob('**/*.mp4'), p.glob('**/*.avi'))):
    print(file)