I have a directory with several types of files. How can I count the number of files in a directory with 2 types of extensions (.txt and .csv)? In my search I found how to count with only one certain extension
3 Answers
A better variant of Yilun answer (which is already nice since it doesn't scan the directory twice like len(glob.glob("*.csv")) and len(glob.glob("*.txt")) would do for instance).
That one doesn't create an extra list (faster) using sum (booleans are summed as 0 or 1) and a generator comprehension:
import os
# get list of files
list_of_files = os.listdir(path)
# txt files
num_txt = sum(x.endswith(".txt") for x in list_of_files)
# csv files
num_csv = sum(x.endswith(".csv") for x in list_of_files)
gencomps+sum is cool, but it still loops/tests twice on list_of_files. Good old loop isn't that bad after all (at least it scans once and shortcuts):
num_txt, num_csv = 0,0
for x in list_of_files:
if x.endswith(".txt"):
num_txt += 1
elif x.endswith(".csv"):
num_csv += 1
BTW to count both at the same time, use the tuple param capability of endswith
# csv & txt files
num_txt_csv = sum(x.endswith((".csv",".txt")) for x in list_of_files)
2 Comments
Jean-François Fabre
you mean python 3.6 ? there's a
scandir backport module for earlier versions.Jean-François Fabre
this "more efficient" thing for
scandir/walk/whatever is faster for a very special set of circumstances: 1) windows 2) if you need to perform a stat on the object. In other cases, it's not faster. Besides, the question is about "files in a directory". walk scans subdirs as well (and so it's slower)
os.walk(dir)