Python glob multiple filetypes Python glob multiple filetypes python python

Python glob multiple filetypes


Maybe there is a better way, but how about:

import globtypes = ('*.pdf', '*.cpp') # the tuple of file typesfiles_grabbed = []for files in types:    files_grabbed.extend(glob.glob(files))# files_grabbed is the list of pdf and cpp files

Perhaps there is another way, so wait in case someone else comes up with a better answer.


glob returns a list: why not just run it multiple times and concatenate the results?

from glob import globproject_files = glob('*.txt') + glob('*.mdown') + glob('*.markdown')


from glob import globfiles = glob('*.gif')files.extend(glob('*.png'))files.extend(glob('*.jpg'))print(files)

If you need to specify a path, loop over match patterns and keep the join inside the loop for simplicity:

from os.path import joinfrom glob import globfiles = []for ext in ('*.gif', '*.png', '*.jpg'):   files.extend(glob(join("path/to/dir", ext)))print(files)