Python: How can I find all files with a particular extension?

try changing the inner loop to something like this

results += [each for each in os.listdir(folder) if each.endswith('.c')]

for _,_,filenames in os.walk(folder):
    for file in filenames:
        fileExt=os.path.splitext(file)[-1]
        if fileExt == '.c':
            results.append(file)

Try "glob":

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']

KISS

# KISS

import os

results = []

for folder in gamefolders:
    for f in os.listdir(folder):
        if f.endswith('.c'):
            results.append(f)

print results

Tags:

Python