2

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

1
  • use os.walk(dir) Commented Mar 22, 2018 at 16:31

3 Answers 3

4

Assume path is the path to your folder. Then

import os

# get list of files
list_of_files = os.listdir(path)

# txt files
num_txt = len([x for x in list_of_files if x.endswith(".txt")])
# csv files
num_csv = len([x for x in list_of_files if x.endswith(".csv")])
Sign up to request clarification or add additional context in comments.

Comments

4

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

you mean python 3.6 ? there's a scandir backport module for earlier versions.
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)
1

You can use regex to filter the filename:

import os
import re

txt_or_csv = [f for f in os.listdir(path) if re.search(r'.*\.(txt|csv)$', f)]

print(len(txt_or_csv))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.