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

前端 未结 13 1983
广开言路
广开言路 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 14:47

    import os, re
    cfile = re.compile("^.*?\.c$")
    results = []
    
    for name in os.listdir(directory):
        if cfile.match(name):
            results.append(name)
    

提交回复
热议问题