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

前端 未结 13 1987
广开言路
广开言路 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:01

    If you replace '.c' with '[.]c$', you're searching for files that contain .c as the last two characters of the name, rather than all files that contain a c, with at least one character before it.

    Edit: Alternatively, match f[-2:] with '.c', this MAY be computationally cheaper than pulling out a regexp match.

提交回复
热议问题