Python 3.5+
Starting with Python version 3.5, the glob
module supports the "**"
directive (which is parsed only if you pass recursive
flag):
import glob
for filename in glob.iglob('src/**/*.c', recursive=True):
print(filename)
If you need an list, just use glob.glob
instead of glob.iglob
.
Python 2.2 to 3.4
For older Python versions, starting with Python 2.2, use os.walk
to recursively walk a directory and fnmatch.filter
to match against a simple expression:
import fnmatch
import os
matches = []
for root, dirnames, filenames in os.walk('src'):
for filename in fnmatch.filter(filenames, '*.c'):
matches.append(os.path.join(root, filename))
Python 2.1 and earlier
For even older Python versions, use glob.glob
against each filename instead of fnmatch.filter
.