How to count files of particular type (like *.txt) in a folder

Thread Solved

Join Date: Oct 2006
Posts: 45
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

How to count files of particular type (like *.txt) in a folder

 
0
  #1
Aug 16th, 2008
Hi
I just want to count files of particular type (like *.txt) in a folder.

I searched net and found this for all files count :
file_count = len(os.walk(valid_path).next()[2])

But I need perticuler type like *.txt . (means how many .txt files in folder)

How to do this ?
Thanks in advance.
Last edited by bimaljr; Aug 16th, 2008 at 6:18 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,157
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 283
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Veteran Poster

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #2
Aug 16th, 2008
You can filter the list like this
  1. file_count = len([f for f in os.walk(".").next()[2] if f[-4:] == ".txt"])
Last edited by Gribouillis; Aug 16th, 2008 at 6:32 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 45
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #3
Aug 16th, 2008
Originally Posted by Gribouillis View Post
You can filter the list like this
  1. file_count = len([f for f in os.walk(".").next()[2] if f[-4:] == ".txt"])
Thanks it works.. but I want the number... not the file list
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,157
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 283
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Veteran Poster

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #4
Aug 16th, 2008
With os.walk(".").next()[2] , you already have a file list. You don't see it because you take it's len(...)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 45
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #5
Aug 16th, 2008
Yes.. You are right again...

Thanks a lot...
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,664
Reputation: vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light 
Solved Threads: 1089
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #6
Aug 16th, 2008
Be aware that your file extension may not always be .txt, but also could be be .TXT or .tXt or txT or such. To get a full count of all of these mixed case extensions it is best to use module glob ...
  1. # to also count mixed case file extensions like .TXT or .Txt etc.
  2. # use module glob ...
  3. import glob
  4.  
  5. # use current directory
  6. # or change with os.chdir(directory_name)
  7. print "Number of .txt files =", len(glob.glob("*.txt"))
Last edited by vegaseat; Aug 16th, 2008 at 8:00 pm. Reason: spelling
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 39
Reputation: Shadow14l is an unknown quantity at this point 
Solved Threads: 7
Shadow14l Shadow14l is offline Offline
Light Poster

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #7
Aug 17th, 2008
I prefer this method

  1. import os
  2. path = ('c:\\stuff\\countfiles\\')
  3. print "Counting all .txt files in: " + path
  4. x=0
  5. for files in os.listdir(path):
  6. if files.endswith('.txt'):
  7. x+=1
  8. print "\nFile #" + str(x) + ": " + files
  9. print "\nTotal number of .txt files in: " + path + " -"
  10. print x
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,157
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 283
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Veteran Poster

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #8
Aug 17th, 2008
try this:
  1. import os, sys
  2. print "Total: %d .txt files" % len(map(lambda f: sys.stdout.write(f+"\n"),sorted(filter(lambda f: f.endswith(".txt"), os.listdir(".")))))
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 45
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

Re: How to count files of particular type (like *.txt) in a folder

 
0
  #9
Aug 18th, 2008
Thanks to all of you.. I just need it... thanks again
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 1269 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC