| | |
How to count files of particular type (like *.txt) in a folder
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 45
Reputation:
Solved Threads: 0
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.
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.
You can filter the list like this
python Syntax (Toggle Plain Text)
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.
•
•
Join Date: Oct 2006
Posts: 45
Reputation:
Solved Threads: 0
•
•
•
•
You can filter the list like this
python Syntax (Toggle Plain Text)
file_count = len([f for f in os.walk(".").next()[2] if f[-4:] == ".txt"])
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)
# to also count mixed case file extensions like .TXT or .Txt etc. # use module glob ... import glob # use current directory # or change with os.chdir(directory_name) 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!
•
•
Join Date: May 2008
Posts: 39
Reputation:
Solved Threads: 7
I prefer this method
python Syntax (Toggle Plain Text)
import os path = ('c:\\stuff\\countfiles\\') print "Counting all .txt files in: " + path x=0 for files in os.listdir(path): if files.endswith('.txt'): x+=1 print "\nFile #" + str(x) + ": " + files print "\nTotal number of .txt files in: " + path + " -" print x
try this:
python Syntax (Toggle Plain Text)
import os, sys print "Total: %d .txt files" % len(map(lambda f: sys.stdout.write(f+"\n"),sorted(filter(lambda f: f.endswith(".txt"), os.listdir(".")))))
![]() |
Similar Threads
- Open In New Window Php (PHP)
- dvdplay.exe (Viruses, Spyware and other Nasties)
- bank system problem !!!! (C++ programming) (C++)
- Read Me:Access to any type of DB's possible (Visual Basic 4 / 5 / 6)
- Parse XML from ASP!!! (ASP)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Tut : Make Your-Self a IP Showing Siggy & hit counter (PHP)
Other Threads in the Python Forum
- Previous Thread: Killing a process outside python
- Next Thread: Running into a weird error
Views: 1269 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client code command convert count create csv ctypes database dictionary django dll error errors examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote replace scrolledtext server socket ssh stamp stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables web windows wxpython






