Count number of files with certain extension in Python

前端 未结 5 786
一整个雨季
一整个雨季 2020-12-12 23:55

I am fairly new to Python and I am trying to figure out the most efficient way to count the number of .TIF files in a particular sub-directory.

Doing some searching,

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 00:49

    For this particular use case, if you don't want to recursively search in the subdirectory, you can use os.listdir:

    len([f for f in os.listdir(myPath) 
         if f.endswith('.tif') and os.path.isfile(os.path.join(myPath, f))])
    

提交回复
热议问题