Count number of files with certain extension in Python

前端 未结 5 758
一整个雨季
一整个雨季 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条回答
  •  抹茶落季
    2020-12-13 00:52

    Something has to iterate over all files in the directory, and look at every single file name - whether that's your code or a library routine. So no matter what the specific solution, they will all have roughly the same cost.

    If you think it's too much code, and if you don't actually need to search subdirectories recursively, you can use the glob module:

    import glob
    tifCounter = len(glob.glob1(myPath,"*.tif"))
    

提交回复
热议问题