Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:34:36+00:00 2026-05-26T02:34:36+00:00

i want to count files in the command directory based on their extension. So,

  • 0

i want to count files in the command directory based on their extension.
So, i created a list with all the files in the cwd ,then a list with only the extensions and then i made a dict from that list.I made the dict with a count parameter but i don’t know how to handle this.My dict looks like “{‘txt’:0,’doc’:0}”.

 import os,glob

def myfunc(self):
    mypath=os.getcwd() 
    filelist=glob.glob("*") #list with all the files in cwd
    extension_list=[os.path.splitext(x)[1][1:] for x in filelist] #make list with the extensions only
    print(extension_list)

    count=0;
    mydict=dict((x,count) for x in extension_list) #make dict with the extensions as keys and count as value
    print(mydict)

    for i in mydict.values():  #i must do sth else here..
        count+=1
    print(count)
    print(mydict)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:34:37+00:00Added an answer on May 26, 2026 at 2:34 am

    Just iterate through the extension list and increment the dictionary value:

    for ext in extension_list:
        mydict[ext] += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find and count all the files on my system that begin
I want to count the number of lines that have all the log files
I have a folder with 2000+ files. I want to count the files by
I want to count the total divs inside a container and toggle their visibilities
Suppose I want to count the lines of code in a project. If all
We've got a PHP application and want to count all the lines of code
I have a directory with XML files. I quickly want to go through them
I want to get the total count of the number of lines from all
I want to work on the files generated by the split command. How do
I want to retrieve a list of the files that has been added or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.