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

前端 未结 13 1948
广开言路
广开言路 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:03
    for _,_,filenames in os.walk(folder):
        for file in filenames:
            fileExt=os.path.splitext(file)[-1]
            if fileExt == '.c':
                results.append(file)
    
    0 讨论(0)
提交回复
热议问题