Python Forum
Counting the number of files related to particular format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting the number of files related to particular format
#4
use defaultdict from collections module

from collections import defaultdict
import os
all_files = defaultdict(list)
for root, folders, files in os.walk(r'C:\Users\sai\Desktop\dosc'):
    for item in files:
        ext = os.path.splitext(item)[-1][1:]
        all_files[ext].append(item)

for ext, files in all_files.items():
    print('Number of {} files: {}'.format(ext, len(files)))
That said, let me point some issues with your code
1. filter is built-in function, don't use it as variable name
2. no need to iterate over filenames. use list.extend() method instead
3. no need to create new list (with odd name newstr, which suggest it is a str object). If you want the len of the list with all files - just use it
4. you create a function for each extension/file type. Although it's not efficient to iterate over the all-files list each time, you can at least do it with one generic function. just pass second argument - the file extension(s).
from pathlib import Path
def get_files(all_files, extensions):
   return [item for item in all_files if Path(item).suffix in extensions]

all_files = []
for root, folders, files in os.walk(r'C:\Users\BKolev\Desktop'):
    all_files.extend(files)

jpeg_files = get_files(all_files, ['.jpeg', '.jpg'])
print('Number of jpeg files: {}'.format(len(jpeg_files)))
here I use pathlib module, instead of os.path to show different tools you can use also in the previous snippet
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Counting the number of files related to particular format - by buran - Nov-05-2018, 08:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read directory listing of files and parse out the highest number? cubangt 5 2,373 Sep-28-2022, 10:15 PM
Last Post: Larz60+
Photo How can I use 2 digits format for all the number? plumberpy 6 2,348 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 1,683 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Although this is a talib related Q it's mostly related to python module installing.. Evalias123 4 5,697 Jan-10-2021, 11:39 PM
Last Post: Evalias123
  Counting Number of Element in a smart way quest 2 2,017 Nov-09-2020, 10:24 PM
Last Post: quest
  multiple number format conversion oli_action 4 2,587 Aug-11-2020, 05:10 AM
Last Post: perfringo
  get method not counting number of strings in dictionary LearningTocode 2 2,116 Jun-13-2020, 11:17 PM
Last Post: LearningTocode
  counting items in a list of number combinations Dixon 2 2,086 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Number format william888 3 2,841 Aug-23-2019, 05:33 AM
Last Post: ThomasL
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,560 Aug-12-2019, 01:31 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020