[Tutor] how to extract number of files from directory

Kent Johnson kent37 at tds.net
Wed Oct 12 20:29:15 CEST 2005


Marc Buehler wrote:
> 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?

os.system() returns the exit code of the program, not the output. You can run an external program and capture the output, but generally you should look for a solution within Python before resorting to external programs. In this case, try

import glob
a = len(glob.glob('*.JPG'))

Kent



More information about the Tutor mailing list