[Tutor] how to extract number of files from directory

Ertl, John john.ertl at fnmoc.navy.mil
Wed Oct 12 20:46:47 CEST 2005


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

 -----Original Message-----
From: 	Marc Buehler [mailto:marc_buehler at yahoo.com] 
Sent:	Wednesday, October 12, 2005 11:10 AM
To:	tutor at python.org
Subject:	[Tutor] how to extract number of files from directory


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?


The apocalyptic vision of a criminally insane charismatic cult leader 


Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list