Tags: allows, directory, easier, extract, files, glob, globjpglist, import, jpg, marc, number, programming, python, stuff, system, type

how to extract number of files from directory

On Programmer » Python

3,867 words with 3 Comments; publish: Sun, 04 May 2008 13:35:00 GMT; (20093.75, « »)

Marc,

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

Message

From: Marc Buehler [mailto:marc_buehler (AT) yahoo (DOT) com]

Sent:Wednesday, 12, 2005 11:10 AM

To:tutor (AT) python (DOT) org

Subject:[Tutor] how to extract number of files from directory

hi.

i'm new to Python

i would like to extract the number of JPG files

from the current directory and use that number

as a parameter in my python script.

i tried:

a = os.system('ls *JPG | wc -l')

when i do:

print a

i get '0'.

what am i missing?

marc

The apocalyptic vision of a criminally insane charismatic cult leader

http://www.marcbuehler.net

Yahoo! Music Unlimited

Access over 1 million songs. Try it free.

Tutor maillist - Tutor (AT) python (DOT) org

Tutor maillist - Tutor (AT) python (DOT) org

All Comments

Leave a comment...

  • 3 Comments
    • 10/12/05, Ertl, John <john.ertl (AT) fnmoc (DOT) navy.milwrote:

      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

      Tutor maillist - Tutor (AT) python (DOT) org

      #1; Sun, 04 May 2008 13:36:00 GMT
    • w chun wrote:

      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?

      glob.glob('*.[jJ][pP][eE]?[gG]') ?

      or maybe (not sure this is legal; I'm pretty sure it's ugly :-)

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

      Kent

      -- 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

      Tutor maillist - Tutor (AT) python (DOT) org

      Tutor maillist - Tutor (AT) python (DOT) org

      #2; Sun, 04 May 2008 13:37:00 GMT
    • glob seems to be a good idea,

      Yep, I got my glob and listdir mixed up earlier

      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?

      Sounds like a job for a regex?

      Alan G.

      Tutor maillist - Tutor (AT) python (DOT) org

      #3; Sun, 04 May 2008 13:38:00 GMT