[Tutor] how to extract number of files from directory

w chun wescpy at gmail.com
Wed Oct 12 21:24:45 CEST 2005


On 10/12/05, Ertl, John <john.ertl at fnmoc.navy.mil> wrote:
>
> You can do most system type stuff in Python and it makes it much easier.
> import glob
> jpgList = glob.glob("*.jpg") # the glob allows you to use wild cards in
> the
> search
> jpgCount = len(jpgList)
> This gives you a list of all files that end in .jpg. You can then do a
> len(jpgList) to get the number of files
> John
>

glob seems to be a good idea, but is there a way to do it
case-insensitively, i.e., .jpg and .JPG?

it may be slower than glob, but i'm finding myself thinking more like...

len([i for i in os.listdir('tmp') if i.lower().endswith('.jpg')])

now, anyone with a solution for .jpg, .JPG, .jpeg, *and* .JPEG?

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2006,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com <http://wescpy-at-gmail.com>
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051012/23ecec61/attachment.html


More information about the Tutor mailing list