943,908 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1793
  • Python RSS
Aug 16th, 2008
0

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

Expand Post »
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 7:18 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006
Aug 16th, 2008
0

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

You can filter the list like this
python Syntax (Toggle Plain Text)
  1. file_count = len([f for f in os.walk(".").next()[2] if f[-4:] == ".txt"])
Last edited by Gribouillis; Aug 16th, 2008 at 7:32 am.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Aug 16th, 2008
0

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

You can filter the list like this
python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006
Aug 16th, 2008
0

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

With os.walk(".").next()[2] , you already have a file list. You don't see it because you take it's len(...)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Aug 16th, 2008
0

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

Yes.. You are right again...

Thanks a lot...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006
Aug 16th, 2008
0

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

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 ...
python Syntax (Toggle Plain Text)
  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 9:00 pm. Reason: spelling
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 17th, 2008
0

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

I prefer this method

python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 8
Light Poster
Shadow14l is offline Offline
39 posts
since May 2008
Aug 17th, 2008
0

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

try this:
python Syntax (Toggle Plain Text)
  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(".")))))
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Aug 18th, 2008
0

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

Thanks to all of you.. I just need it... thanks again
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Killing a process outside python
Next Thread in Python Forum Timeline: Running into a weird error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC