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

前端 未结 13 2004
广开言路
广开言路 2020-12-13 14:05

I am trying to find all the .c files in a directory using Python.

I wrote this, but it is just returning me all files - not just .c files.

13条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 15:00

    KISS

    # KISS
    
    import os
    
    results = []
    
    for folder in gamefolders:
        for f in os.listdir(folder):
            if f.endswith('.c'):
                results.append(f)
    
    print results
    

提交回复
热议问题